LeetCode:Contains Duplicate
2016-07-26 00:58
411 查看
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.
这里使用set查找,较为方便,快排会超时!
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
set<int> st;
set<int>::iterator itor;
int len = nums.size();
for (int i = 0; i < len; ++i) {
itor = st.find(nums[i]);
if (itor != st.end()) return true;
else st.insert(nums[i]);
}
return false;
}
};
twice in the array, and it should return false if every element is distinct.
这里使用set查找,较为方便,快排会超时!
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
set<int> st;
set<int>::iterator itor;
int len = nums.size();
for (int i = 0; i < len; ++i) {
itor = st.find(nums[i]);
if (itor != st.end()) return true;
else st.insert(nums[i]);
}
return false;
}
};
相关文章推荐
- ruby 数组使用教程
- Ruby中的数组和散列表的使用详解
- C#实现AddRange为数组添加多个元素的方法
- C#比较二个数组并找出相同或不同元素的方法
- set 命令特殊用法
- C#动态调整数组大小的方法
- 详解Lua中的数组概念知识
- Perl中的列表和数组学习笔记
- 探索PowerShell (八) 数组、哈希表(附:复制粘贴技巧)
- C#中数组初始化与数组元素复制的方法
- You must SET PASSWORD before executing this statement的解决方法
- C#交错数组用法实例
- Linux Shell 数组建立及使用技巧
- PowerShell数组的一些操作技巧
- C#通过yield实现数组全排列的方法
- C#不重复输出一个数组中所有元素的方法
- C#实现将数组内元素打乱顺序的方法
- 批处理 Set 命令详解 让你理解set命令第1/2页
- C#中用foreach语句遍历数组及将数组作为参数的用法
- ASP中set与dim的区别(自己的理解)