Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
这里使用set查找,较为方便,快排会超时!
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
set<int> st;
set<int>::iterator itor;
int len = nums.size();
for (int i = 0; i < len; ++i) {
itor = st.find(nums[i]);
if (itor != st.end()) return true;
else st.insert(nums[i]);
}
return false;
}
};