publicList<Integer>findDisappearedNumbers(int[] nums) {List<Integer> res =newArrayList<>();if (nums ==null||nums.length==0) {return res; }int n =nums.length;for (int i =0; i < n; i++) {res.add(nums[i]); } for (int i =0; i < n; i++) {int curNum = nums[i];res.set(curNum -1, curNum); }// remove from the backfor (int i = n -1; i >=0; i--) {if (res.get(i) == i +1) {res.remove(i); } else {res.set(i, i +1); } }return res; }
publicList<Integer>findDisappearedNumbers(int[] nums) {// Iterate over each of the elements in the original arrayfor (int i =0; i <nums.length; i++) {// Treat the value as the new indexint newIndex =Math.abs(nums[i]) -1;// Check the magnitude of value at this new index// If the magnitude is positive, make it negative // thus indicating that the number nums[i] has // appeared or has been visited.if (nums[newIndex] >0) { nums[newIndex] *=-1; } }// Response array that would contain the missing numbersList<Integer> result =newLinkedList<Integer>();// Iterate over the numbers from 1 to N and add all those// that have positive magnitude in the arrayfor (int i =1; i <=nums.length; i++) {if (nums[i -1] >0) {result.add(i); } }return result; }