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.
Subscribe to see which companies asked this question
思路
按我的做法,就是开一个map,存元素及出现次数,若大于1,则return true.
但在leetcode热门解答上发现一个一行代码的判断方法。
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
return nums.size()>set<int>(nums.begin(),nums.end()).size();
}
};