<LeetCode OJ>Contains Duplicate【217】
2015-12-31 22:18
429 查看
217. Contains Duplicate
My SubmissionsQuestion
Total Accepted: 58633 Total
Submissions: 149186 Difficulty: Easy
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.
Subscribe to see which companies asked this question
Hide Tags
Array Hash
Table
Hide Similar Problems
(E) Contains Duplicate II (M)
Contains Duplicate III
//思路首先:用数据结构set来做,因为他是红黑树为底层,所以查找某个元素时间浮渣度较低,并且set不允许相同元素出现 //如果某个元素的count为0则,插入到set,如果不为0,return false //set的查找时间为O(lg(N))所以最终时间浮渣度为O(Nlg(N)) class Solution { public: bool containsDuplicate(vector<int>& nums) { if(nums.empty()) return false; set<int> s; s.insert(nums[0]); for(int i=1;i<nums.size();i++) { if(!s.count(nums[i])) s.insert(nums[i]); else return true; } return false; } };
相关文章推荐
- 详解Paint的各种set方法
- POJ - 1422 Air Raid 二分图最大匹配
- 自己总结 RTAI编译
- RTAI安装
- aircrack-ng on OSX 从零开始
- 解决Ubuntu14.04中VMware workstaion无法打开
- 职责链模式(chain of responsibility)
- 职责链模式(chain of responsibility)
- poj 1198 hdu 1401 搜索+剪枝 Solitaire
- Your PC needs to be repaired
- 字符串匹配算法比较 http://blog.csdn.net/airfer/article/details/8951802/
- <LeetCode OJ>Factorial Trailing Zeroes【172】
- Paiting Series 1: Part 2
- Paiting Series 1: Part 1
- TrainCascade类图解析
- async & await 的前世今生
- Saitama Destroys Hotel
- INSTALL_FAILED_MEDIA_UNAVAILABLE错误处理
- 解决sublime Text3无法安装插件问题 -- There are no packages available for installation
- uboot中raise:Signal #8 caught的根本原因