码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode217]Contains Duplicate

时间:2016-01-18 22:53:42      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:

题目: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.

查找一个数组中是否含有重复元素,可以利用哈希实现

代码:

class Solution {
public:
    bool containsDuplicate(std::vector<int>& nums) {
        std::set<int> s;
        for (int i = 0; i < nums.size(); ++i)
        {
            s.insert(nums[i]);
        }
        return s.size() != nums.size();
    }
};

 

[LeetCode217]Contains Duplicate

标签:

原文地址:http://www.cnblogs.com/zhangbaochong/p/5140660.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!