难度:简单 题目描述 解题思路 跟442. 数组中重复的数据 简直是一模一样的嘛。442最后要求返回重复的数字,这道题要求返回没出现过的数字,按照方法最后重复的数字占的下标就是本来应该出现的数字。
比如:[4,3,2,7,8,2,3,1] 一趟标记之后是:【-4 -3 -2 -7 8 2 -3 -1 】 还是正数的下标就是没出现过的数字 5,6
public static List<Integer> findDuplicates1(int[] nums) { List<Integer> re = new LinkedList<>(); for (int i = 0; i < nums.length; i++) { int index = Math.abs(nums[i]); if (nums[index - 1] > 0) { //大于0说明没出现过,出现过就变成负数标记 nums[index - 1] *= -1; } } for (int i = 0; i < nums.length; i++) { if(nums[i] > 0) re.add(i+1); } return re; }