448. Find All Numbers Disappeared in an Array

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.

Example:

Input:
[4,3,2,7,8,2,3,1]

Output:
[5,6]

本题解法和first missing positive的解法有点类似,只是没有开始的那个分割,代码如下:
 1 public class Solution {
 2     public List<Integer> findDisappearedNumbers(int[] nums) {
 3         List<Integer> res = new ArrayList<>();
 4         for(int i=0;i<nums.length;i++){
 5             if(nums[i]>nums.length) continue;
 6             int num = Math.abs(nums[i]);
 7             nums[num-1]=nums[num-1]>=0?(-1)*nums[num-1]:nums[num-1];
 8         }
 9         for(int i=0;i<nums.length;i++){
10             if(nums[i]>0) res.add(i+1);
11         }
12         return res;
13     }
14 }
原文地址:https://www.cnblogs.com/codeskiller/p/6401340.html