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 ...
随机推荐
- Python中pass、continue、break、exit()的区别
pass :不做任何事情,只起到占位的作用 continue: 跳出本次循环 break:结束循环 exit():结束整个程序 由于continue和break较简单,这里就不给出代码
- 洛谷-P2661 信息传递——有向图中的最小环
题意 给定一个 $n$ 个结点有向图,求其中最小环的大小.($n \leq 200000$). 分析 由于每条点出度都为1且满足传递性,可以用并查集做. 如果有一条从x到y的有向边,那么y就是x的父亲 ...
- 使用自签CA,Server,client证书和双向认证
服务端代码 package main import ( "crypto/tls" "crypto/x509" "google.golang.org/g ...
- (尚009)Vue列表渲染
变异方法:说白了就是对原方法进行了包装,包装后实现了2个功能1:实现原方法的功能;2.更新界面. 1.test009.html <!DOCTYPE html><html lang=& ...
- 【JQuery】操作前端控件知识笔记
一.jQuery操作复选框checkbox 1.设置选中.取消选中.获取被选中的值.判断是否选中等 注意:操作checked.disabled.selected属性,强制建议只用prop()方法!!, ...
- printf的使用
#!/bin/bashprintf "|------------------------------------\n"printf "this is printf str ...
- c函数指针和指针函数如何使用何定义;如何调用使用
#include <stdio.h> int * sum(int x); //声明一个 指针函数 返回类型位一个指针变量 可以通过*p来获取值 int (*pfun)(int,int);/ ...
- github提示Permission denied (publickey),如何才能解决?
参考: https://my.oschina.net/u/1377923/blog/1822038 https://www.cnblogs.com/chjbbs/p/6637519.html
- Spring boot 解决跨域问题
import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.we ...
- Hadoop hadoop 机架感知配置
机架感知脚本 使用python3编写机架感知脚本,报存到topology.py,给予执行权限 import sys import os DEFAULT_RACK="/default-rack ...