041. First Missing Positive

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.