Simple solution in JAVA


  • 0
    P

    Here's a simple solution in JAVA:

    class Solution {
        public int firstMissingPositive(int[] nums) {
            if(nums.length==0){
                return 1;
            }
                 
            Arrays.sort(nums);
            int cnt=1;
            int prevVal = Integer.MIN_VALUE;
            for(int i=0; i<nums.length; i++){            
                int val = nums[i];
                if(val>0 && prevVal!=val){
                    if(val-cnt>0){
                        break;
                    }
                    cnt++;
                }
                prevVal = val;
            }
            return cnt;
        }
    }
    

Log in to reply
 

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