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.
方法很简单:用map记录即可。
class Solution { public: bool containsDuplicate(vector<int>& nums) { map<int,int> kvmap; for(vector<int>::iterator iter=nums.begin(); iter!=nums.end(); iter++) { kvmap[*iter]++; if(kvmap[*iter]>1) return true; } return false; } };
时间: 2024-12-17 23:40:14