[Leetcode] First Missing Positive a missing positive number

Source: Internet
Author: User

Given an unsorted integer array, find the first missing positive integer.

For example,
Given [1,2,0] return 3 ,
and [3,4,-1,1] return 2 .

Your algorithm should run in O(n) time and uses constant space.

This problem let us find the first positive number of missing, because of the limit O (n) time, so the general sorting method can not be used, at first I did not see also limited the space complexity, so think of a hash table to solve, this idea is very simple, the first time the group put all the numbers in the hash table, and find the maximum value , the next cycle increments from 1 to find the number, which number is not found to return which number, if the largest number has been found, then return the maximum value of +1, the code is as follows:

//Not constant spaceclassSolution { Public:    intFirstmissingpositive (intA[],intN) {if(N <=0)return 1; Unordered_map<int,int>m; intMX = a[0];  for(inti =0; I < n; ++i) {if(A[i] >0) {M[a[i]]=1; MX=max (MX, a[i]); }        }         for(inti =1; I <= MX; ++i) {if(M.find (i) = = M.end ())returni; }        returnMX +1; }};

But the above solution is not O (1) time complexity, so we need to think of another solution, since we can not create a new array, then we can only overwrite the original array, our idea is to put 1 in the first position of the array a[0],2 placed in the second position a[1], that needs to put A[i] a[a[i] 1 ], then we traverse the entire array, if a[i]! = i + 1, while A[i] is an integer and not greater than N, and A[i] is not equal to a[a[i]-1] Then we will change the position of the two, if we do not meet the above conditions directly skip, and finally we go through the array again, If the number on the corresponding location is incorrect, the correct number is returned, and the code is as follows:

classSolution { Public:    intFirstmissingpositive (intA[],intN) {inti =0;  while(I <N) {if(A[i]! = i +1&& A[i] >0&& A[i] <= n && a[i]! = A[a[i]-1]) {swap (A[i], A[a[i]-1]); } Else {                ++i; }        }         for(i =0; I < n; ++i) {if(A[i]! = i +1)returni +1; }        returnn +1; }};

[Leetcode] First Missing Positive a missing positive number

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.