Share my Java O(n) solution.


  • 0
    L

    Is the space complexity O(1)?

    public List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> ret = new ArrayList<Integer>();    
        if(nums == null||nums.length==0) return ret;
    	    
        for(int i = 0; i < nums.length; i++)    ret.add(i+1);
        for(int i = 0; i < nums.length; i++)    ret.set(nums[i]-1,0);
        ret.removeAll(Arrays.asList(0));
        return ret;
    }
    

Log in to reply
 

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