2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_219.java 902 Bytes
一键复制 编辑 原始数据 按行查看 历史
Charies Gavin 提交于 2020-02-06 12:44 . 初始化 myleetcode 项目
package com.hit.basmath.learn.hash_table;
import java.util.HashSet;
import java.util.Set;
/**
* 219. Contains Duplicate II
* <p>
* 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.
* <p>
* Example 1:
* <p>
* Input: nums = [1,2,3,1], k = 3
* Output: true
* <p>
* Example 2:
* <p>
* Input: nums = [1,0,1,1], k = 1
* Output: true
* <p>
* Example 3:
* <p>
* Input: nums = [1,2,3,1,2,3], k = 2
* Output: false
*/
public class _219 {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Set<Integer> set = new HashSet<Integer>();
for (int i = 0; i < nums.length; i++) {
if (i > k) set.remove(nums[i - k - 1]);
if (!set.add(nums[i])) return true;
}
return false;
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助