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 ...
随机推荐
- sha1加密java代码
sha1 加密 java代码 public static String getSha1(String str){ if(str==null||str.length()==0){ return null ...
- 关于SVN配置文件的一个小例子
1 背景假设 厦门央瞬公司是一家电子元器件设备供应商,其中有个ARM部门,专门负责ARM芯片的方案设计.销售,并在北京.上海各设立了一个办事处.对于工作日志,原先采用邮件方式发给经理,但是这种方式 ...
- 泛泰A850 (高通8064+720p)刷4.4专用中文recovery TWRP2.7.1.3版
欢迎关注泛泰非盈利专业第三方开发团队 VegaDevTeam (本team 由 syhost suky zhaochengw(z大) xuefy(大星星) tenfar(R大师) loogeo cr ...
- Java整型数组的最大长度到底有多长?
Java整型数组的最大长度到底有多长? 今天上网查了一下,各种说法都有,这个问题似乎总困扰我们Java初学者,无奈,只好自己试了一下,以下是我的测试代码,如果有错误,还望不吝赐教! 使用eclipse ...
- apache2.4.4启用deflate压缩
今天在看<高性能php应用开发>这本书,说道如何启用mod_deflate: 启用如下模块: LoadModule deflate_module modules/mod_deflate.s ...
- Android正在使用Handler实现信息发布机制(一)
上一篇文章,我们谈到了电话Handler的sendMessage方法,最后,我们将进入一个电话 sendMessageAtTime方法,例如下列: public boolean sendMessage ...
- php laravel 帧 该文件上传
好,我承认我的忠告. 今天laravel框架编写一个文件上传部分.总能找到不正确的路径.但是,终于攻克. 以下我分享一下自己的学习体会吧. client <form method="P ...
- android插件化-apkplug框架启动-02
本文章基于apkplug v1.6.7 版本号编写,最新方式以官网最新消息为准 一 apkplug框架所须要的库文件(宿主) 可从http://git.oschina.net/plug/apkplug ...
- Node.js 博客实例(五)编辑与删除功能
原教程 https://github.com/nswbmw/N-blog/wiki/_pages的第五章,因为版本号等的原因,在原教程基础上稍加修改就可以实现. 如今给博客加入编辑文章与删除文章的功能 ...
- Android含文档server结束(client UI接口异步请求的一部分)三
在本文中,AsyncTask为了实现异步请求,详细代码如下所示的: public class downloadActivity extends Activity { private TextView ...