my java solution, O(n) time and not use extra space.


  • 1
    L
    public List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> list = new ArrayList<Integer>();
        int val = 0,index = 0;
        for(int i = 0; i < nums.length; i++){
            index = Math.abs(nums[i]) - 1;
            val = Math.abs(nums[index]);
            nums[index] = -val;
            
        }
        for(int i = 0; i < nums.length; i++){
            if(nums[i] > 0){
                list.add(i+1);
            }
        }
        return list;
    }

Log in to reply
 

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