【LeetCode】217. Contains Duplicate 解题小结

题目:

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.

这题利用unordered_map来解决应该还是很简单的。

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_map<int, int> map;
        for (int i = 0; i < nums.size(); i++){
            if (map.find(nums[i]) != map.end()){
                return true;
            }
            else{
                map[nums[i]] = i;
            }
        }
        return false;
    }
};

 

posted on 2016-08-27 10:24  医生工程师  阅读(111)  评论(0编辑  收藏  举报

导航