public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> res = new ArrayList<>();
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 back
for (int i = n - 1; i >= 0; i--) {
if (res.get(i) == i + 1) {
res.remove(i);
} else {
res.set(i, i + 1);
}
}
return res;
}
public List<Integer> findDisappearedNumbers(int[] nums) {
// Iterate over each of the elements in the original array
for (int i = 0; i < nums.length; i++) {
// Treat the value as the new index
int 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 numbers
List<Integer> result = new LinkedList<Integer>();
// Iterate over the numbers from 1 to N and add all those
// that have positive magnitude in the array
for (int i = 1; i <= nums.length; i++) {
if (nums[i - 1] > 0) {
result.add(i);
}
}
return result;
}