Java code O(n) time constant space


  • 0
    V
    public List<Integer> findDisappearedNumbers(int[] nums) {
            List<Integer> result = new ArrayList<>();
            int temp = 0;
            for(int i = 0; i < nums.length; i++)
            {
                if(nums[i] - 1 != i && nums[nums[i] - 1] != nums[i])
                {
                    temp = nums[nums[i] - 1];
                    nums[nums[i] - 1] = nums[i];
                    nums[i] = temp;
                    i--;
                }
            }
            for(int i = 0; i < nums.length; i++) if(nums[i] - 1 != i) result.add(i + 1);
            return result;
        }
    

Log in to reply
 

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