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 ...
随机推荐
- 程序设计入门——C语言 第4周编程练习 2 念整数(5分)
题目内容: 你的程序要读入一个整数,范围是[-100000,100000].然后,用汉语拼音将这个整数的每一位输出出来. 如输入1234,则输出: yi er san si 注意,每个字的拼音之间有一 ...
- Deep Learning 13_深度学习UFLDL教程:Independent Component Analysis_Exercise(斯坦福大学深度学习教程)
前言 理论知识:UFLDL教程.Deep learning:三十三(ICA模型).Deep learning:三十九(ICA模型练习) 实验环境:win7, matlab2015b,16G内存,2T机 ...
- nginx 负载均衡策略
nginx 负载均衡策略 1. 轮询轮询方式是nginx负载均衡的默认策略,根据每个server的权重值来轮流发送请求,例如:upstream backend {server backend1.e ...
- springmvc__SimpleUrlHandlerMapping(对访问地址进行加工,以键值对的形式)
1.配置web.xml(这里配置url-pattern为/) <!-- 编码过滤器 --> <filter> <filter-name>characterEncod ...
- Octopus系列之价格计算公式
百分比:在原价的基础上优惠5%,相当于95%折销售实际价格:最后实际销售的价格[基准价格:如果有特价就基于特价,没有特价基于原价]优惠金额:减少的金额 批量优惠 百分比: 原价*(1-5%)的算法实际 ...
- Java:多线程<四> Lock、停止线程、守护线程、join、优先级&yield
Java1.5以后,Condition将Object监视器方法(wait, notify, notifyAll)分解成截然不同的对象,以便通过这些对象与任意Lock实现组合使用为每个对像提供多个等待s ...
- HTTP-Session工作机制
HTTP-Session将HTTP这种无状态协议通过session来保存状态.然而session通过记录服务器生成编号来标识协议,从而服务器就可以识别协议的状态. session保存服务器端, co ...
- Citrix 虚拟化笔记
利用win7 x64/Vmware workstation 10练习Citrix虚拟化 [安装Xenserver 6.2] 1)硬盘空间不足:要求最小8G 2)不支持硬件虚拟化:找到建立的XENSER ...
- android recyclerview 更新ui
http://blog.csdn.net/leejizhou/article/details/51179233
- android baseActivity
package newdemo.jeno.designdemo.activitynew; import android.os.Bundle;import android.support.annotat ...