本人是蒟弱,一开始没想到 y 总的做法,先到了一个 map
的做法。
class Solution {
public:
int duplicateInArray(vector<int>& nums) {
map<int, bool> d;
short n = nums.size();
for (auto x : nums)
if (x < 0 || x >= n)
return -1;
for (auto x : nums) {
if (d.count(x))
return x;
d[x] = true;
}
return -1;
}
};
本人蒟弱,欢迎各大佬来喷。