public class DayCode {
public static void main(String[] args) {
int[] nums = new int[]{4,3,2,7,8,2,3,1};
List<Integer> ans = new DayCode().findDisappearedNumbers(nums);
System.out.println("ans is " + ans);
}
/**
* https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/
* @param nums
* @return
*/
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> ans = new ArrayList<>();
int[] arr = new int[nums.length + 1];
for (int num : nums) {
arr[num] = 1;
}
for (int i = 1; i < arr.length; i++) {
if (arr[i] == 0) {
ans.add(i);
}
}
return ans;
}
/**
*
* @param nums
* @return
*/
public List<Integer> findDisappearedNumbers2(int[] nums) {
List<Integer> ans = new ArrayList<>();
int n = nums.length;
for (int num : nums) {
int x = (num - 1) % n;
nums[x] += n;
}
for (int i = 0; i < n; i++) {
if (nums[i] <= n) {
ans.add(i + 1);
}
}
return ans;
}
}
评论