LeetCode_219. Contains Duplicate II
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.
Example 1:
Input: nums = [1,2,3,1], k = 3
Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1
Output: true
Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
package leetcode.easy;
public class ContainsDuplicateII {
public boolean containsNearbyDuplicate(int[] nums, int k) {
java.util.HashMap<Integer, Integer> map = new java.util.HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i]) && (i - map.get(nums[i]) <= k)) {
return true;
} else {
map.put(nums[i], i);
}
}
return false;
}
@org.junit.Test
public void test() {
int[] nums1 = { 1, 2, 3, 1 };
int[] nums2 = { 1, 0, 1, 1 };
int[] nums3 = { 1, 2, 3, 1, 2, 3 };
int k1 = 3;
int k2 = 1;
int k3 = 2;
System.out.println(containsNearbyDuplicate(nums1, k1));
System.out.println(containsNearbyDuplicate(nums2, k2));
System.out.println(containsNearbyDuplicate(nums3, k3));
}
}
LeetCode_219. 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 ...
随机推荐
- luogu 2312 解方程 乱搞+取模
思路非常好想,但是你很难想到去用这个算法,因为这个几乎就是个乱搞~ 我们发现多项式中每一个系数都很大,但是 $m$ 却很小,即最多只用 $10^6$ 个整数需要验证. 我们知道,如果一个数等于 $0$ ...
- RookeyFrame 删除 线下添加的model
环境:在model层已经添加了Crm_Cm_ContactInfo2 这个类,这个类现在已经添加到了数据库的,使用之前的方法(在前面的文章有提到该类) 删除步骤: 1.Sys_Module表 的字段 ...
- 在Xilinx ISE中生成ROM时需要注意的事
在Xilinx ISE中生成ROM时,需要指定.coe文件.需要做到两件事.其一,要使用memory_initialization_radix= ; memory_initializatoin_vec ...
- [golang]text/template模板
这个可以用来处理text文本,不过我更偏爱做成代码生成器. [golang]text/template模板 package main import ( "os" "tex ...
- Python之☞网络编程中一些概念问题(未完)
:::一些名词的解释::: 网络: 网络是辅助双方能够连接在一起的工具,使用网络的目的,为了联通多方然后进行通讯,能够让软件在不同的电脑上运行,相互传输数据. 网络协议: 约定俗成的,没有理由. TC ...
- vim配图
https://blog.csdn.net/zhlh_xt/article/details/52458672 https://www.jianshu.com/p/75cde8a80fd7 https: ...
- [WEB安全]XSS命令总结
一:正常构造方式: 1.无过滤,直接写: <script>alert(1)</script> 2.正常截断: "> <script>alert(1) ...
- Linux压缩和解压类指令
一.gzip / gunzip 指令 gzip 用于压缩文件,gunzip 用于解压文件. 基本语法gizp 文件 (功能描述:只能将文件压缩为*.gz文件)gunzip 文件.gz (功能描述 ...
- go语言中type的几种使用
type是go语法里的重要而且常用的关键字,type绝不只是对应于C/C++中的typedef.搞清楚type的使用,就容易理解go语言中的核心概念struct.interface.函数等的使用.以下 ...
- 安卓开发实战之app之版本更新升级(DownloadManager和http下载)完整实现
转载: https://blog.csdn.net/u013278099/article/details/52692008 前言 本文将讲解app的升级与更新.一般而言用户使用App的时候升级提醒有两 ...