My own java solution, easy to understand.


  • -1
    J
    public class Solution {
        public int firstMissingPositive(int[] nums) {
            int max = 0;
            for(int i : nums){
                max = i > max ? i : max;
            }
            boolean[] bool = new boolean[max+2];
            Arrays.fill(bool, false);
            
            for(int i : nums){
                if(i >= 0)
                    bool[i] = true;
            }
            
            int result = 0;
            
            for(int i = 1; i < max + 2; i++){
                if(bool[i] == false){
                    result = i;
                    break;
                }
            }
            return result;
        }
    }

  • 0
    B

    The space complexity is O(n) not constant space. For this case, [-1, 1000],it needs new boolean[1002] boolean array. :(


Log in to reply
 

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