解题思路:
既然N+1个数长度为2N,那么只要出现大于2的就是重复的数,返回即可,用map进行记录,代码如下:
class Solution {
public:
int repeatedNTimes(vector<int>& nums) {
unordered_map<int, int> mp;
for(auto& num : nums) {
mp[num] ++;
if(mp[num] > 1) return num;
}
return 0;
}
};