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.
思路1.
对每一个数字都与其后序的数字进行比较,如果相同则返回true,如果不同,则指导遍历完最后一个数字返回false,时间复杂度为(n-1)+(n-2)+....2+1,所以是O(n2),代码就不写了,这方法不好。
思路2.
先排序,然后在遍历vector,如果有相邻的值相同,则返回true,负责将pivot设置为当前的值。总的时间复杂度为排序O(nlogn)+遍历O(n)
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
bool flag = false;
int len = nums.size();
if(len > 1)
{
sort(nums.begin(),nums.end());
int MarkNum = nums[0];
for(int i = 1; i<= len; i++)
{
if(nums[i] == MarkNum)
flag = true;
else
MarkNum=nums[i];
}
}
else
flag = false;
return flag;
}
};
思路3,利用额外空间,unordered_map,如果元素不存在,计数器的值加1,如果存在,则返回true
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_map<int,int> cmpMap;
for( int i = 0; i < nums.size(); i++ ){
if( cmpMap.find(nums[i]) == cmpMap.end() ){
cmpMap[nums[i]]++;
}
else{
return true;
}
}
return false;
}
};
思路3我本来以为时间复杂度应该在O(n)但提交以后,速度还没思路2的快,不知道是什么原因。
LeetCode【217. Contains Duplicate】的更多相关文章
- LeetCode【第1题】Two Sum
准备刷一刷LeetCode了. 题目: ''' Given an array of integers, return indices of the two numbers such that they ...
- LeetCode 【31. Next Permutation】
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode 【47. Permutations II】
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 【190. Reverse Bits】
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- LeetCode【169. Majority Element】
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode OJ 217.Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- LeetCode【112. 路径总和】
思路就是从根节点开始向下选节点,依次与sum比较大小,若小,则向下选左右节点其中一个,若大,则接下来判断是否是叶子节点,若是,则返回false 若不是,则上一步选另一节点,再将上述重新执行. 对于叶子 ...
- LeetCode【101. 对称二叉树】
对称二叉树,就是左节点的左节点等于右节点的右节点,左节点的右节点等于右节点的左节点. 很自然就想到迭代与递归,可以创建一个新的函数,就是另一个函数不断的判断,返回在主函数. class Solutio ...
- leetcode 【 Linked List Cycle 】 python 实现
题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ...
随机推荐
- Decorator
1 意图:动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator模式相比生成子类更灵活. 2 别名:包装器Wrapper 3 动机:将组件嵌入到另一个对象中,由这个对象添加边框.嵌入的 ...
- Knockout学习笔记之二($root,$parent及$data的区别)
以下是我从Google上找到的一个例子,非常生动形象,我修改了部分代码,具体内容如下: 对于$root 与$parent的区别: $root refers to the view model appl ...
- 如何浏览并管理 Outlook 邮件?
当你的邮件多起来的时候你就不得不考虑这个问题了,如何处理各种邮件? 如何浏览并管理 Outlook 邮件? 待续~
- python核心编程第六章练习6-11
6-11.转换.(a)创建一个从整型到IP地址的转换,如下格式:www.xxx.yyy.zzz.(b)更新你的程序,使之可以逆转换.[答案](a)代码如下: Input_number = abs(in ...
- swig之于c++
[namespace] namespace nsTest1 { int nsAdd(int a, int b) { return a + b; } } namespace nsTest2 { int ...
- miniui设置边框的方法
if (field == "loginname") { if (record._id == 2) { e.cellHtml = ""; e.cellStyle ...
- c#简易反射调用泛型方法
// 所谓程序集的简单理解,存在不同项目中(不是解决方案),即using前需要引用**.dll 1.调用当前类文件下的方法public List<T> GetByCondition< ...
- call(),apply(),bind()与回调
1.call(),apply(),bind()方法 JavaScript 中通过call或者apply用来代替另一个对象调用一个方法,将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定 ...
- csu 1804 有向无环图
题目地址 分析:从复杂度来看,一定不可能是枚举和来计算.1e5的规模来看,应该是复杂度比较合适. 我是这么想的,对于三个点,假设1->2有a种走法,2->3有b种走法.那么1->3应 ...
- 0429 Scrum团队成立与第6-7章读后感
Scrum团队成立: 团队名称:何首污大战污妖王 团队目标:每个人都尽可能的学到东西,共同进步. 团队口号:因为自信,所以成功! 团队照: 角色分配 产品负责人(梁毅乾): 决定开发内容和优先级排序, ...