Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements of [1, n] inclusive that do not appear in this array.
Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

有的出现两次,有的出现一次。找出没有出现的。

public class Solution {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> res=new ArrayList<Integer>();
        if(nums==null||nums.length==0){
            return res;
        }
        //第一个初始化一个
        // int n=nums.length;
        // int[] num=new int[n];
        // Arrays.fill(num,-1);
        // for(int i=0;i<n;i++){
        //     num[nums[i]-1]=nums[i];
        // }
        // for(int i=0;i<n;i++){
        //     if(num[i]<0){
        //         res.add(i+1);
        //     }
        // }
        ///直接在原数组上搞。如果出现过,将其本来应该在的位置设置为负值。最后遍历,为正的即为缺失值
        for(int i=0;i<nums.length;i++){
            int temp=Math.abs(nums[i])-1;
            if(nums[temp]>0){
                nums[temp]=-nums[temp];
            }
        }
        for(int i=0;i<nums.length;i++){
            if(nums[i]>0)
            res.add(i+1);
        }
         return res;
    }
    
   
}

PS:参考网上的用标志位来做。先初始化一个-1的数组。将数字放回到其原来应该存在的位置。最后遍历,若小于0则是缺失的。。。。。。。。。。。。。。。。。。。。。。。。。。

PPS:直接在原数组操作,将其应该出现的位置设置成现在数字的负数,最后遍历,大于0的就是缺失的值。