A simple Java solution using hashmaps


  • 0
    K
    public int firstMissingPositive(int[] nums) {
          Map<Integer, Boolean> dict = new HashMap<Integer, Boolean>();
          if(nums.length==0)
            return 1;
          int max = 0;
          for(int i=0; i<nums.length;i++){
            if(nums[i]<0)
                continue;
            if(nums[i]>=max)
              max = nums[i];
            dict.put(nums[i], true);
          }
          for(int i=1; i<max+2; i++){
            if(!dict.containsKey(i))
              return i;
          }
          return 1;
        }
    

Log in to reply
 

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