leetcode_Contains Duplicate_easy

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

leetcode_Contains Duplicate_easy的相关文章

leetcode_Contains Duplicate II_easy

Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. 方法很简单:和之前的那道题目类似,也是用map. class Solution { public: boo