/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
using namespace std;
struct LinkedlistNode{
LinkedlistNode *next;
int val;
};
int main()
{
LinkedlistNode *n1 = new LinkedlistNode();
n1->val = 3;
LinkedlistNode *n2 = new LinkedlistNode();
n2->val = 5;
n1->next = n2;
LinkedlistNode *n3 = new LinkedlistNode();
n3->val = 8;
n2->next = n3;
LinkedlistNode *n4 = new LinkedlistNode();
n4->val = 3;
n3->next = n4;
LinkedlistNode *n5 = new LinkedlistNode();
n5->val = 10;
n4->next = n5;
LinkedlistNode *n6 = new LinkedlistNode();
n6->val = 2;
n5->next = n6;
LinkedlistNode *n7 = new LinkedlistNode();
n7->val = 1;
n6->next = n7;
n7->next = nullptr;
int x = 5;
LinkedlistNode *p1 = n1, *p2 = n1;
loop:
while(p1 && p1->val < x){
p1 = p1->next;
p2 = p2->next;
}
//p2 = p2->next;
while(p2){
if(p2->val < x){
int temp = p1->val;
p1->val = p2->val;
p2->val = temp;
p1 = p1->next;
goto loop;
}
p2 = p2->next;
}
p1 = n1;
while(p1){
cout<<p1->val<<" ";
p1 = p1->next;
}
return 0;
}
Given a blacklist B containing unique integers from [0, N) , write a function to return a uniform random integer from [0, N) which is NOT in B . Optimize it such that it minimizes the call to system’s Math.random() . Note: 1 <= N <= 1000000000 0 <= B.length < min(100000, N) [0, N) does NOT include N. See interval notation . Example 1: Input: ["Solution","pick","pick","pick"] [[1,[]],[],[],[]] Output: [null,0,0,0] Example 2: Input: ["Solution","pick","pick","pick"] [[2,[]],[],[],[]] Output: [null,1,1,1] Example 3: Input: ["Solution","pick","pick","pick"] [[3,[1]],[],[],[]] Output: [null,0,0,2] Example 4: Input: ["Solution","pick","pick","pick"] [[4,[2]],[],[],[]] Output: [null,1,3,1] Explanation of Input Syntax: The input is two lists: the subroutines called and their argume...
Comments
Post a Comment