Jan 11 - Contains Duplicate II; Array; Traverse; HashMap; HashSet;

代码:

public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
//int gap = nums.length;
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
Integer j = map.put(nums[i], i);
if(j != null){
int diff = i - j;
if(diff <= k) return true;
}
}
return false;
}
}

HashMap 和 HashSet 相关函数的信息:

https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html

https://msdn.microsoft.com/en-us/library/bb353005(v=vs.110).aspx

时间: 2024-10-05 05:05:59

Jan 11 - Contains Duplicate II; Array; Traverse; HashMap; HashSet;的相关文章

Jan 11 - Contains Duplicate; Array; Traverse; Integer;

遍历数组找出有没有重复的元素 首先想到用一个数组记录出现的元素的个数 代码: public class Solution { public boolean containsDuplicate(int[] nums) { if(nums.length == 0) return true; int[] checkElement = new int[Integer.MAX_VALUE]; for(int i = 0; i < nums.length; i++){ if(checkElement[num

[LeetCode] Contains Duplicate &amp; Contains Duplicate II

Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 1 class Solution { 2

LeetCode之“散列表”:Contains Duplicate &amp;&amp; Contains Duplicate II

 1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 代码如下:

【LeetCode】Contains Duplicate II

Contains Duplicate II 问题描述 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. 算法思想 思想一: 两个数组下标i,j,在叫j-i<

【LeetCode-面试算法经典-Java实现】【219-Contains Duplicate II(包含重复元素II)】

[219-Contains Duplicate II(包含重复元素II)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 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] = n

LeetCode:Contains Duplicate II - 判断数组内是否有重复元素2

1.题目名称 Contains Duplicate II(判断数组内是否有重复元素2) 2.题目地址 https://leetcode.com/problems/contains-duplicate-ii/ 3.题目内容 英文: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] = nu

[LeetCode][JavaScript]Contains Duplicate II

Contains Duplicate II Given an array of integers and an integer k, return true if and only if 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. https://leetcode.com/proble

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 difference between i and j is at most k. 题目大意: 找到数组中两个相同元素,如果这两个元素的距离小于等于k

[LeetCode] Contains Duplicate(II,III)

Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 解题思路 用一个set保存数组中的值,如