class Solution { public int findRepeatNumber(int[] nums) { int tem; for(int i=0;i < nums.length;i++){ //如果坑位跟萝卜不匹配 while(nums[i] != i){ //如果别的坑位元素已经有他所属的萝卜 if(nums[i] == nums[nums[i]]){ return nums[i]; } tem = nums[i]; nums[i] = nums[tem]; nums[tem] = tem; } } return -1; } }
int repeat = -1; Set
setl = new HashSet (); for(int num:nums){ repeat = num; if(!setl.add(num)){ return repeat; } }
return repeat;
