https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/
染色法,遍历一遍将位于abs(nums[i])位置处的值置为负数。再遍历一次,值仍然为正的位置就是不存在的值。
public class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
LinkedList<Integer> res = new LinkedList<>();
if (nums == null || nums.length <= 1) {
return res;
}
for (int i = 0; i < nums.length; i++) {
if (nums[Math.abs(nums[i]) - 1] > 0) {
nums[Math.abs(nums[i]) - 1] = -nums[Math.abs(nums[i]) - 1];
}
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
res.add(i + 1);
}
}
return res;
}
}