I think some test case is missing. Or maybe the problem assumes there are no duplicates in the array right?


  • 0
    N
    class Solution {
    public:
        int firstMissingPositive(int A[], int n) {      //if the array is[1,2,2,2,4],this can't give us 3 right?However the code got an AC
            for(int i=0;i<n;i++){     
                
                while(A[i]!=i+1&&A[i]<n&&A[i]>0){
                    if(A[i]==A[A[i]-1])   break;
                    swap(A[i],A[A[i]-1]);
                }
            }
            
            for(int i=0;i<n;i++){
                if(A[i]!=i+1) return i+1;
            }
            return n+1;
        }
    };

  • 0
    Z

    Your code gets result of 3, I think it is right for the case of 1 2 2 2 4


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.