My solution, resemble to First Missing Positive solution.


  • 0
    E
    public class Solution {
        public List<Integer> findDisappearedNumbers(int[] nums) {
            if(nums == null || nums.length == 0 || nums.length == 1)
                return new ArrayList<Integer>();
            for(int i = 0; i < nums.length; i++) {
                while(nums[nums[i] - 1] != nums[i]) {
                    swap(nums, i, nums[i] - 1);
                }
            }
            List<Integer> results = new ArrayList<>();
            for(int i = 0; i < nums.length; i++) {
                if(nums[i] != i + 1)
                    results.add(i + 1);
            }
            return results;
        }
        
        private void swap(int[] nums, int i, int j) {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }
    }
    

Log in to reply
 

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