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。在常数时间内找到元素。这道题学习到的两个经验:

  1. 当想在常数时间内完毕查询工作时考虑hash表。

  2. 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的更多相关文章

  1. [leetcode] Contains Duplicate II

    Contains Duplicate II Given an array of integers and an integer k, find out whether there there are ...

  2. leetcode:Contains Duplicate和Contains Duplicate II

    一.Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your fun ...

  3. 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II

     217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...

  4. Contains Duplicate,Contains Duplicate II,Contains Duplicate III

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

  5. LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II

     1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...

  6. 217/219. Contains Duplicate /Contains Duplicate II

    原文题目: 217. Contains Duplicate 219. Contains Duplicate II 读题: 217只要找出是否有重复值, 219找出重复值,且要判断两者索引之差是否小于k ...

  7. [LeetCode] Contains Duplicate & Contains Duplicate II

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

  8. 219. Contains Duplicate II【easy】

    219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...

  9. [LeetCode] Contains Duplicate(II,III)

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

随机推荐

  1. Java中对不变的 data和object reference 使用 final

    Java中对不变的 data和object reference 使用 final 许多语言都提供常量数据的概念,用来表示那些既不会改变也不能改变的数据,java关键词final用来表示常量数据.例如: ...

  2. 通过 Jersey Http请求头,Http响应头,客户端 API 调用 REST 风格的 Web 服务

    原地址:http://blog.csdn.net/li575098618/article/details/47853263 Jersey 1.0 是一个开源的.可以用于生产环境的 JAX-RS(RES ...

  3. hdu4126(最小生成树+dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4126 题意:给出一幅3000个点的图,有10000次操作: 求将某条边的权值变大后的最小生成树,最后输 ...

  4. 用Tomcat和Eclipse开发Servlet程序

    1. 安装eclipse 1). 在官网上直接下载Eclipse IDE for Java EE Developers,解压即可: 2. eclipse安装tomcat插件: 1). 在http:// ...

  5. HashMap源码解读(转)

    http://www.360doc.com/content/10/1214/22/573136_78188909.shtml 最近朋友推荐的一个很好的工作,又是面了2轮没通过,已经是好几次朋友内推没过 ...

  6. jar包有嵌套的jar的打包成jar的方法

    1.先写一个类,将其打包成jar包. 代码如下: package com.wjy.jar; public class GetUserName { public String getUserName() ...

  7. Android在Context详细解释 ---- 你不知道Context

                                                                                                         ...

  8. SDL2源码分析5:更新纹理(SDL_UpdateTexture())

    ===================================================== SDL源码分析系列文章列表: SDL2源码分析1:初始化(SDL_Init()) SDL2源 ...

  9. 在Amazon AWS RHEL 7上安装 配置PPTP VPN

    0 前言 0.1 为什么需要VPN? 国内的VPN不是必须,但是国外的VPN是很有用的.连接到国外的VPN服务器之后就可以访问Google,Facebook, Youtube等网站,没有Google的 ...

  10. Java实现BASE64编解码器

    Java实现BASE64编解码器 笔者:chszs,转载注明.博客首页:http://blog.csdn.net/chszs BASE64和其它类似的编码算法通经常使用于转换二进制数据为文本数据,其目 ...