算法—找出数组中重复的数字
题目
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
方法1:HashMap
使用HashMap,将数字和它出现的次数作为<key,value>
代码:
import java.util.HashMap; public class Solution { public boolean duplicate(int numbers[],int length,int [] duplication) { if(numbers==null || length==0){ return false; } //使用HashMap,将数字和它出现的次数作为<key,value> HashMap<Integer,Integer> map = new HashMap<>(); for(int i=0; i<length; i++){ if(map.containsKey(numbers[i])){ //该数字出现过 duplication[0] = numbers[i]; //第一个重复的数字放入duplication[0] return true; }else{ map.put(numbers[i],1); } } return false; } }
方法2:hash思想,但不添加额外空间
题目中有个条件是:长度为n的数组里的所有数字都在0到n-1的范围内。
可以把数组看作一个下标与数字一一对应的数组,即i位置上对应的数字也是i。这样的话,重复的数字必定占位不止一个。
如果下标为i的位置上存放的数字不是i,而是a[i],那么判断在a[i]位置上的数字是不是也是a[i],如果是,则a[i]就是重复的数字,如果不是,则交换a[i]和a[a[i]]。
代码:
import java.util.HashMap; public class Solution { public boolean duplicate(int numbers[],int length,int [] duplication) { if(numbers==null || length==0){ return false; } //将数组看作下标和存放数字一一对应的数组 int temp; for(int i=0; i<length; i++){ while(numbers[i] != i){ //如果下标和数值不对应 if(numbers[i] == numbers[numbers[i]]){ //numbers[i]就是重复的数字 duplication[0] = numbers[i]; return true; }else{ //交换两数,使得numbers[i]在它应该在的位置numbers[i]上 temp = numbers[numbers[i]]; numbers[numbers[i]] = numbers[i]; numbers[i] = temp; } } } return false; } }