Giving runtime error here works fine in my compiler


  • 0
    H
    int firstMissingPositive(vector<int>& nums) {
        if(nums.size()==0){
            return 1;
        }
        int n=nums.size();
        int j=0;
        for(int i=0;i<n;i++){
    
            if(nums[i]<=0){
                int a=nums[i];
                nums[i]=nums[j];
                nums[j]=a;
                j++;
            }
        }
    
        for(int i=j;i<n;i++){
    
            if(abs(nums[i])+j-1<n && nums[abs(nums[i])-j-1]>0 ){
                nums[abs(nums[i])-j-1]=-nums[abs(nums[i])-j-1];
            }
        }
    
        for(int i=j;i<n;i++){
            if(nums[i]>0){
    
                return i-j+1;
            }
        }
        return n-j+1;
        
    }

Log in to reply
 

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