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 ...
随机推荐
- iOS 推送通知处理
//这是程序杀死后再通过点击通知进入时调用的方法 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpti ...
- [必会] 表单验证+弹框~老司机原生js
<!DOCTYPE html><html><head> <meta charset="gb2312"> <title>恰 ...
- golang strings
package main import s "strings" //别名 import ( "fmt" ) var p = fmt.Println func m ...
- iOS开发 ReactiveCocoa入门教程 第一部分
作为一个iOS开发者,你写的每一行代码几乎都是在响应某个事件,例如按钮的点击,收到网络消息,属性的变化(通过KVO)或者用户位置的变化(通过CoreLocation).但是这些事件都用不同的方式来处理 ...
- laravel开发微信公众号1 之基本配置
需要用到的packagist: https://github.com/overtrue/laravel-wechat ( 目前最优雅的laravel微信sdk) 首先安装 compose ...
- /etc/rc.d/与/etc/rc.d/init.d的关系
/etc/init.d指向/etc/rc.d/init.d目录 . 除了直接调用脚本外(如/etc/rc.d/init.d/xinetd),还可以用service命令来控制init.d目录下的服务如 ...
- HashMap其实就那么一回事儿之源码浅析
上篇文章<LinkedList其实就那么一回事儿之源码分析>介绍了LinkedList, 本次将为大家介绍HashMap. 在介绍HashMap之前,为了方便更清楚地理解源码,先大致说说H ...
- IE11里边form拦截失效,永远被弹回登录页
现象描述: 1.在某些服务器上发布了程序以后,用IE11去浏览程序(试了多台电脑都一样),发现总是登录不进去,因为登录之后总是被立即反弹回登录页面,就像是登录后写入的票据瞬间丢失一样. 2.但是同一套 ...
- guava学习--FluentIterable
public class FluentIterableTest { public static void main(String[] args) { Man man1 = new Man(" ...
- NetworkComms V3 之支持TCP连接和UDP连接
NetworkComms V3 无缝的支持TCP连接和UDP连接. 您可以很容易的创建这两种连接 //创建一个连接信息对象 ConnectionInfo connInfo = ); //创建一个TCP ...