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 ...
随机推荐
- yii学习笔记
学而不思则罔,思而不学则殆,适度的总结有利于学习效果的提升. 以前就是埋头看书很少动手所以学习效果不好. 学习yii的原因是自己基本功差,但是yii的学习本身也需要成本
- Js获取下拉框选定项的值和文本
Js获取下拉框的值和文本网上提供了2种方法:但有些人很不负责任,他们根本没考虑到浏览器之间的差异导致的错误,导致很多新手琢磨了半天找不出错误! 下面我总结下Firefox和IE下获取下拉框选定项的值和 ...
- hdu3228Island Explorer
链接 给你两条线及两条线上的点,求最小生成树. 可以挨个枚举一条线上的点,三分出另一条线上离他最近的点进行连边. 注意N.M可能为0 debug了1天半,至今不知道原始二分版本错在哪里.. #incl ...
- C++ 不能在类体外指定关键字static
C++ static 函数的问题 近日读 C++ primer 中static 一章 , 有这么一句话, “静态成员函数的声明除了在类体中的函数声明前加上关键字static 以及不能声明为const ...
- css-九宫格自适应的实现
高度自适应使用padding 或 padding-bottom + 百分比来实现: 宽度自适应使用width + 百分比来实现. 下面是实现九宫格自适应的代码: <!DOCTYPE html&g ...
- hdu 1532(最大流)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- [转]深入理解Java 8 Lambda(类库篇——Streams API,Collectors和并行)
以下内容转自: 作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文链接:http://zh.lucida.me/blog/java-8-lambdas-insideout-l ...
- 【转载】 ionic 的 下拉刷新 与 上拉加载
这篇文章是讲解 Ioinc中怎么实现 下拉刷新和上拉加载的.也是我们日常做项目是必不可少的功能.有兴趣的小伙伴可以来学习一下. 更多关于 IONIC 的资源: http://www.aliyue.ne ...
- MVN使用随笔
001 创建项目 mvn archetype:generate -DgroupId=com.company.push.monitor -DartifactId=push-monitor -Darche ...
- ROS 使用自带和usb摄像头获取图像
笔记本自带的摄像头的设备号一般为/dev/video0 第一步:安装Webcam 驱动 $ sudo apt-get install git-core $ cd ~/catkin_ws/src $ g ...