A neat java solution (10ms)


  • 0
    class Solution {
        public int firstMissingPositive(int[] nums) {
            int j=0;
            while(j<nums.length){
                if(nums[j]<=0||nums[j]>=nums.length||nums[nums[j]-1]==nums[j]) j++;
                else {
                    int key = nums[j];
                    nums[j] = nums[key-1];
                    nums[key-1] = key;
                }
            }
            int i=1;
            while(i<nums.length+1&&nums[i-1]==i) i++;
            return i;
        }
    }
    

Log in to reply
 

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