short java Solution O(1) space complexity,2ms


  • -1
    T

    '''
    public class Solution {
    public int firstMissingPositive(int[] nums) {

        List<Integer> list=new ArrayList();
        for(int k:nums){
            if(k>0){
                list.add(k);
            }
        }
        int missing=list.size()+1;
        boolean[] tag=new boolean[list.size()+2];
        for(int k:list){
            if(k<=list.size()){
                tag[k]=true;
            }
        }
        for(int i=1;i<tag.length;i++){
            if(!tag[i]){
                return i;
            }
        }
        return missing;
    }
    

    }
    '''


  • 0
    E

    Your solution is not O(1) space. It uses list and tag and those spaces could be linear to the size of input.


Log in to reply
 

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