小小程序媛  
得之坦然,失之淡然,顺其自然,争其必然

题目

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.

分析

判断所给整数序列中有无重复元素。

AC代码

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        if (nums.empty())
            return false;

        unordered_set<int> us;
        int len = nums.size();

        for (int i = 0; i < len; ++i)
        {
            if (us.count(nums[i]) != 0)
                return true;
            else
                us.insert(nums[i]);
        }
        return false;

    }
};
posted on 2015-12-03 12:46  Coding菌  阅读(111)  评论(0编辑  收藏  举报