LeetCode 219. Contains Duplicate II (包含重复项之二)
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
题目标签:Array, Hash Table
题目给了我们一个nums array 和一个 k, 让我们找到两个 相同的项,并且它们的index 差值 小于等于 k。
这次我们依然要利用Hash Table,因为这次需要比较它们的index, 所以需要key 和value, 不同于 包含重复项之一 那一题,只需要比较数字。
遍历nums array, 如果nums[i] 不在hash table 里的话,就把 nums[i] 当作key, i 当作 value 存入hash table;
如果hash table 有nums[i]的话,就比较两个index 的差值,如果小于等于 k, 就返回true。
Java Solution:
Runtime beats 52.19%
完成日期:05/27/2017
关键词:Array, Hash Table
关键点:利用Hash Table, 把nums[i] 当作key, i 当作value 存入Hash Table
public class Solution
{
public boolean containsNearbyDuplicate(int[] nums, int k)
{
if(nums.length == 0 || nums == null)
return false; HashMap<Integer, Integer> unique = new HashMap<>(); for(int i=0; i<nums.length; i++)
{
if(unique.containsKey(nums[i]) && i - unique.get(nums[i]) <= k)
{
return true;
}
else
unique.put(nums[i], i);
} return false;
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 219. Contains Duplicate II (包含重复项之二)的更多相关文章
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
- [LeetCode] Contains Duplicate II 包含重复值之二
Given an array of integers and an integer k, return true if and only if there are two distinct indic ...
- LeetCode 217. Contains Duplicate (包含重复项)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- [LeetCode] 220. Contains Duplicate III 包含重复元素 III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- C#解leetcode 219. Contains Duplicate II
该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...
- 219 Contains Duplicate II 存在重复 II
给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使 nums [i] = nums [j],并且 i 和 j 的绝对差值最大为 k. 详见:https://leetcod ...
- LeetCode 219 Contains Duplicate II
Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...
- Java for LeetCode 219 Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i ...
随机推荐
- Java:java中BufferedReader的read()及readLine()方法的使用心得
BufferedReader的readLine()方法是阻塞式的, 如果到达流末尾, 就返回null, 但如果client的socket末经关闭就销毁, 则会产生IO异常. 正常的方法就是使用sock ...
- expect实现scp/ssh-copy-id非交互
expect工具可以实现自动应答,从而达到非交互的目的. expect具体使用用法比较复杂,中文手册我正在翻译中,以后翻译完了做了整理再补.本文只有几个ssh相关最可能用上的示例. yum -y in ...
- SIT 和 UAT
在企业级软件的测试过程中,经常会划分为三个阶段--单元测试,SIT和UAT,如果开发人员足够,通常还会在SIT之前引入代码审查机制(Code Review)来保证软件符合客户需求且流程正确.下面简单介 ...
- 1 Spring Cloud Eureka服务治理(下)
注:此随笔为读书笔记.<Spring Cloud微服务实战> 上篇主要介绍了什么是微服务以及微服务治理的简单实现,如微服务注册中心的实现.微服务注册的实现.微服务的发现和消费的实现.微服务 ...
- ACM学习之路___HDU 5723(kruskal + dfs)
Abandoned country Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s) ...
- GitHub使用(三) - GitHub安装及初步使用
1. 首先下载Windows版本“GitHubSetup.exe”如下,下载地址为:https://desktop.github.com/
- Java 中与(&)短路与(&&)以及 或(|)短路或(||)的关系
一.逻辑运算符的使用 1)逻辑运算符的连接的是布尔表达式,要与位运算符做区分. 2)使用方法: public class Test { public static void main(String[ ...
- Redis 的安装与使用
环境:CentOS 6.6 Redis 版本:redis-3.0 (考虑到 Redis3.0 在集群和性能提升方面的特性,rc 版为正式版的候选版,而且很快就出正式版) 安装目录:/usr/local ...
- clone github报Permission denied (publickey) 解决方案
问题描述 问题产生的原因,不是很清楚,就不管了.在执行git clone git@github.com:****.git 的时候报了Permission denied (publickey). War ...
- centos7下安装tesseract-ocr进行验证码识别
摘要: centos7安装依赖库 tesseract配置 代码例子 centos7安装依赖库 安装centos系统依赖 yum install -y automake autoconf libtool ...