LeetCode219:Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.
直接使用循环时间复杂度为O(N*k),使用stl中的基于红黑树实现的map能降低循环的时间,在O(logN)的时间复杂度内找到元素。或者更进一步使用基于hash表的unordered_map。在常数时间内找到元素。这道题学习到的两个经验:
- 当想在常数时间内完毕查询工作时考虑hash表。
- stl中实用hash表实现的数据结构map和set,所以它们也保留了hash表查询上的特点。
代码例如以下:
class Solution {
public:
/* 解法一:超时
bool containsNearbyDuplicate(vector<int>& nums, int k) {
int length=nums.size();
if(length<=1||k<=0) return false;
//将每个元素与其后面k个元素进行比較
for(int i=0;i<length;i++)
{
for(int j=1;j<=k&&(i+j)<length;j++)
{
if(nums[i]==nums[i+j])
return true;
}
}
return false;
}
*/
//解法二,利用stl中的map。记录下整数以及它的下标
bool containsNearbyDuplicate(vector<int>& nums, int k)
{
//之前直接使用的是map。时间是96ms,后来把map替换成unordered_map时间变成了32ms
unordered_map<int ,int> maps;
int length=nums.size();
for(int i=0;i<length;i++)
{
if(maps.count(nums[i]))//假设在maps中找到了该元素。推断它们位置是否小于等于k
{
if((i-maps[nums[i]])<=k)
return true;
}
maps[nums[i]]=i;
}
return false;
}
};
版权声明:本文博主原创文章,博客,未经同意不得转载。
LeetCode219:Contains Duplicate II的更多相关文章
- [leetcode] Contains Duplicate II
Contains Duplicate II Given an array of integers and an integer k, find out whether there there are ...
- leetcode:Contains Duplicate和Contains Duplicate II
一.Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your fun ...
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
- Contains Duplicate,Contains Duplicate II,Contains Duplicate III
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II
1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...
- 217/219. Contains Duplicate /Contains Duplicate II
原文题目: 217. Contains Duplicate 219. Contains Duplicate II 读题: 217只要找出是否有重复值, 219找出重复值,且要判断两者索引之差是否小于k ...
- [LeetCode] Contains Duplicate & Contains Duplicate II
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- 219. Contains Duplicate II【easy】
219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...
- [LeetCode] Contains Duplicate(II,III)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
随机推荐
- 基于 Apache Mahout 构建社会化推荐引擎
基于 Apache Mahout 构建社会化推荐引擎 http://www.ibm.com/developerworks/cn/views/java/libraryview.jsp 推荐引擎利用特殊的 ...
- 有向无环图(DAG)的最小路径覆盖
DAG的最小路径覆盖 定义:在一个有向图中,找出最少的路径,使得这些路径经过了所有的点. 最小路径覆盖分为最小不相交路径覆盖和最小可相交路径覆盖. 最小不相交路径覆盖:每一条路径经过的顶点各不相同.如 ...
- c++ cin>>详解
参考地址:http://www.cnblogs.com/A-Song/archive/2012/01/29/2331204.html 程序的输入都建有一个缓冲区,即输入缓冲区.一次输入过程是这样的,当 ...
- SVN的命令解析(感觉不错就转了)
本文链接: http://www.php-oa.com/2008/03/12/svnminglingzailinuxxiadeshiyong.html .将文件checkout到本地目录 svn ch ...
- 编译安装LNMP Centos 6.5 x64 + Nginx1.6.0 + PHP5.5.13 + Mysql5.6.19
(来自:http://www.cnblogs.com/vicowong/archive/2011/12/01/2116212.html) 环境: 系统硬件:vmware vsphere (CPU:2* ...
- Nginx安装手冊以及图片server部署
1. 安装gcc yum install gcc 2. 安装pcre,pcre-devel 在zhoulh文件夹下建立source build文件夹 mkdir source bu ...
- Gitclient使用
1 首次安装gitclient msysgit watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3h4NTA0NjU5OTg3/font/5a6L5L2T/ ...
- [状压dp] hdu 4064 Carcassonne
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4064 Carcassonne Time Limit: 3000/1000 MS (Java/Othe ...
- 苹果公司的新的编程语言 Swift 高级语言(十一)--初始化类的析构函数的一个实例
一 .实例的初始化 实例的初始化是准备一个类.结构或枚举的实例以便使用的过程. 初始化包含设置一个实例的每个存储属性为一个初始值,以及运行不论什么其他新的实例可以使用之前须要的设置或 ...
- HTTP请求WebTool
/// <summary> /// 执行HTTP POST请求. /// </summary> /// <param name="url">请求 ...