Leetcode NO.217 Contains Duplicate
2015-06-25 04:28
465 查看
本题题目要求如下:
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.
我的算法很直接,就是用hashset,遍历数组,如果该元素已经存在于hashset中,则是重复的,如果不在hashset中,则继续插入
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.
我的算法很直接,就是用hashset,遍历数组,如果该元素已经存在于hashset中,则是重复的,如果不在hashset中,则继续插入
class Solution { public: bool containsDuplicate(vector<int>& nums) { unordered_set<int> hashset; for (int i = 0; i < nums.size(); ++i) { if (hashset.find(nums[i]) != hashset.end()) { return true; } else { hashset.insert(nums[i]); } } return false; } };
相关文章推荐
- leetcode 11 -- Container With Most Water
- rails 4 中 因为secret key 引起在production环境下无法访问 应用的解决办法
- TrainActivity、DetailActivity未完,待续
- hdu1026(linxingqiangglai)
- 1216: 斐波那契数列
- 基于Qt有限状态机人工智能的一种实现及改进方法
- Contains Duplicate II
- Climbing Stairs
- Mysql_Faq: ERROR 1396 (HY000): Operation CREATE USER failed for ‘username’@’hostname’
- Leetcode 219 Contains Duplicate II
- 逆向怎么玩 - 动态调试jetBrains CLion实录
- container_of ( )
- LeetCode 217. Contains Duplicate
- UVALive 7138 The Matrix Revolutions(Matrix-Tree + 高斯消元)(2014 Asia Shanghai Regional Contest)
- Factorial Trailing Zeroes
- 关于类似Gmail邮件撤销功能实现初探
- Leetcode 220 Contains Duplicate III
- NSBundle的使用,注意mainBundle和Custom Bundle的区别
- JetBrain WebStorm 注册码
- leetcode-11Container With Most Water