Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,8,2,3,1] Output: [2,3]
存在一个整数数组,其中的全部元素都位于1~n之间,其中n是数组的长度。有的元素出现了一次,而有的元素出现了两次。找到数组中全部出现两次的数字。java
为了在O(N)的时间内找到全部的出现两次的数字,其核心要求在于用现有的数组记录已经访问过的元素,同时不会丢失还没有访问过的元素。思路一采用交换的核心思想,即每次都将当前下标上的值和以该值为下标的位置上的值进行交换,若是该值下标位置上的值和其相等,则说明该数字已经被遍历过一遍了。数组
代码以下:app
public List<Integer> findDuplicates(int[] nums) { int index = 0; List<Integer> result = new ArrayList<Integer>(); while(index < nums.length) { int num = nums[index]; if(num == 0){ index++; }else if (nums[num-1] == num) { if(index != num-1){ result.add(num); nums[index] = 0; } index++; }else{ swap(index, num-1, nums); } } return result; } public void swap(int i, int j, int[] nums) { int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; }
有没有一种办法在既保留当前位置上的值nums[i]的同时,又可以用某种方式记录i+1是否已经被访问过了?能够经过取反的方法来记录是否被访问过这个状况。若是访问到下标为i的位置上的值,则去判断nums[nums[i]-1]位置上的值是否为负数,若是是,则说明num[i]出现了两次,不然将nums[nums[i]-1]位置上的值取反保留。this
代码以下:spa
public List<Integer> findDuplicates(int[] nums) { List<Integer> res = new ArrayList<>(); for (int i = 0; i < nums.length; ++i) { int index = Math.abs(nums[i])-1; if (nums[index] < 0) res.add(Math.abs(index+1)); nums[index] = -nums[index]; } return res; }