首页 > 学院 > 开发设计 > 正文

Leetcode 217 - Contains Duplicate(hash)

2019-11-08 19:50:15
字体:
来源:转载
供稿:网友

题意

判断一个数组中是否有元素出现了2次或以上。

思路

用一个unordered_set记录一下就好。

代码

class Solution {public: bool containsDuplicate(vector<int>& nums) { unordered_set<int> has; for (auto x : nums) { if (has.count(x)) return true; has.insert(x); } return false; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表