Created
January 6, 2018 20:40
-
-
Save jianminchen/6d95fc3a91cdd19f57f02e0439a502bc to your computer and use it in GitHub Desktop.
Get a different number - peer code review
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
int getDifferentNumber( const vector<int>& arr ) | |
{ | |
for(int i = 0; i < arr.size(); i++){ | |
if(Math.Abs(arr[i]) < arr.size()){ | |
arr[arr[i]] = -arr[arr[i]]; | |
} | |
} | |
for(int i = 0;i < arr.size(); i++){ | |
if(arr[i]>=0) | |
{ | |
return i; | |
} | |
} | |
return arr.size(); | |
} | |
int main() { | |
vector<int> input{1, 3, 2, 5}; | |
int ret=getDifferentNumber(input); | |
cout<<ret<<endl; | |
return 0; | |
} | |
/* | |
[-2,-3,-5,1,3,4,5,6,7,8,9] | |
[] 6 | |
[1, 3, 2, 5] -> 0 | |
arr[1]=-arr[1] | |
[1, 3, 2, 0, 100] -> 4 | |
array int[] visit -> only save the number if number < arr's length | |
0 -> visit[0] = 1 | |
4 -> visit[4] = 1 | |
[0,10000] | |
size of 10000 array | |
*/ | |
// first to find the min number of this array, then ,keep increment this number until i got a | |
// number that not belongs to this array, | |
// creat an array with the same size of the input array as arrray tmp, and for each element a, | |
// if this element is less than the size of the input array, and then i just flag the tmp[a]. | |
// after that , i iterate tmp array from begining to end , to find the first that did not be | |
// flagged. this index is the one we need. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment