1 classSolution {2 Public:3 intFirstmissingpositive (vector<int>&nums) {4 Bucketsort (nums);5 //Put the subscript as I place the value of i+1 element, the output is not satisfied with the first nums[i], it is possible to satisfy, it is the last element of the array + 16 for(inti =0; I < nums.size (); ++i) {7 if(Nums[i]! = i +1)returni +1;8 } 9 returnNums.size () +1;Ten } One Private: A voidBucketsort (vector<int>&nums) - { - //The general idea is to swap (A[i], A[a[i]-1]), when a[i] = i + 1 o'clock, do not need to exchange, ++i the //when a[i] > N, or A[i] <= 0, you should choose not to process the current element, because there is no exchangeable element subscript, and the possible subsequent interchange process will place it wherever possible - //when a[i] > i + 1 o'clock, swap () places the current element and all subsequent swapped elements in the correct position (if the subsequent interchange elements meet the constraints) - //when A[i] < i + 1 o'clock, because the elements before I are sorted well, so there will be nums[i] = = Nums[nums[i]-1], always remain unchanged, go ahead + + I - for(inti =0; I < nums.size (); ++i) { + while(Nums[i]! = i +1) { - if(Nums[i] > nums.size () | | nums[i] <=0|| Nums[i] = = Nums[nums[i]-1]) Break; + Else { ASwap (Nums[i], nums[nums[i]-1]); at } - } - } - } -};
041. First Missing Positive