Java simple solution


  • 1
    Y
    public class Solution {
    public int firstMissingPositive(int[] nums) {
        int max = 0;
        if(nums.length == 0){
            return 1;
        }
        for(int i= 0; i<nums.length;i++){
            if(max<=nums[i]){
                max = nums[i];
            }
        }
        int[] check = new int[max+1];
        for(int i=0;i<nums.length;i++){
            check[0] = 1;
            if(nums[i]>0){
            check[nums[i]] = 1;
            }
        }
        for(int i=0;i<max;i++){
            if(check[i]==0){
                return i;
            }
        }
        return max+1;
    }
    

    }


  • 1
    G

    Well, this is not constant space.


Log in to reply
 

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