Easy-understanding Java solution


  • -6
    Y
    public static int firstMissingPositive(int[] nums) {
        Arrays.sort(nums);
        int cnt = 1;
        for(int i = 0 ; i < nums.length; i++){
            if(nums[i] > 0){
                if(nums[i] == cnt - 1 ) continue;
                else if(nums[i] != cnt) return cnt;
                else cnt++;
            }
        }
        return cnt;
    }

  • 0
    B

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


Log in to reply
 

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