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

 1 public class Solution
 2 {
 3     public boolean containsNearbyDuplicate(int[] nums, int k)
 4     {
 5         if(nums.length == 0 || nums == null)
 6             return false;
 7
 8         HashMap<Integer, Integer> unique = new HashMap<>();
 9
10         for(int i=0; i<nums.length; i++)
11         {
12             if(unique.containsKey(nums[i]) && i - unique.get(nums[i]) <= k)
13             {
14                 return true;
15             }
16             else
17                 unique.put(nums[i], i);
18         }
19
20
21         return false;
22     }
23 }

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

时间: 2024-12-07 05:49:31

LeetCode 219. Contains Duplicate II (包含重复项之二)的相关文章

[LeetCode] 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. 这道题是之前那道Contains Duplicate 包含重复值的延伸,不同之处在于那道题只要我们

LeetCode 217. 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. 题目标签:Array, Hash Table 题目给了我们一个nums arr

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 217 Contains Duplicate(包含重复数字)(Vector、hash)

翻译 给定一个整型数字数组,找出这个数组是否包含任何重复内容. 如果任何值出现了至少两次,那么返回真(true), 如果每个值都是互不相同的,那么返回假(false). 原文 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 s

leetcode—217 Contains Duplicate(包含重复的数)

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">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 t

C#解leetcode 219. Contains Duplicate II

该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet 用法. 题目: 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 t

Java [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 difference between i and j is at most k. 解题思路: HashMap解决. 代码如下: public class Solution { public

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 and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. 解题思路: HashMap,JAVA实现如下: public boolean containsNearby

(easy)LeetCode 219.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 iand j is at most k. 方法1:暴力解法 代码如下: public class Solution { public boolean