【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_set来判断元素是否重复出现。


代码实现:

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_set<int> my_set;
        for (auto &e : nums){
            if (my_set.find(e) != my_set.end()){
                return true;
            }else{
                my_set.insert(e);
            }
        }
        
        return false;
    }
};

原创文章 299 获赞 2 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zxc120389574/article/details/106066909
今日推荐