题解 | #数组中重复的数字#
数组中重复的数字
http://www.nowcoder.com/practice/6fe361ede7e54db1b84adc81d09d8524
// write code here
int res=0;
int n=numbers.length;
if(n==0)
res=-1;
else{
int []temp=new int [n];
for(int i=0;i<n;i++){
temp[numbers[i]]++;
}
for(res=0;res<n;res++){
if(temp[res]>=2)
break;
}
}
return res;
}
题中意,即,总会有重复的数字,而且数字范围在(0,n-1)之间,数组长度n。所以思路很简单,遍历一遍原数组,用新数组记录原数组中数字出现的次数,最后再遍历到值大于等于2的位置,输出这个位置的下标。