题目
C++代码
class Solution {
public:
int findRepeatNumber(vector<int>& nums){
set<int> numsSet;
for(int i = 0; i < nums.size(); i++){
if(numsSet.find(nums[i]) == numsSet.end()){
numsSet.insert(nums[i]);
}
else{
return nums[i];
}
}
return -1;
}
};
Java代码
class Solution {
public int findRepeatNumber(int[] nums) {
Set<Integer> set = new HashSet<Integer>();
int repeat = -1;
for (int num : nums) {
if (!set.add(num)) {
repeat = num;
break;
}
}
return repeat;
}
}
如果有任何问题可以邮箱联系我:raymondlam1@yeah.net