219. Contains Duplicate II java solutions

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 jis at most k.

Subscribe to see which companies asked this question

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

AC 代码不是很好

时间: 2024-10-10 04:34:17

219. Contains Duplicate II java solutions的相关文章

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

119. Pascal&#39;s Triangle II java solutions

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 1 public class Solution { 2 public List<Integer> getRow(int rowIndex) { 3

47. Permutations II java solutions

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations: [ [1,1,2], [1,2,1], [2,1,1] ] 1 public class Solution { 2 List<List<Integer>> ans

63. Unique Paths II java solutions

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

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

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

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, 让

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. 巧妙地利用字典 class Solution(object): def containsNearbyDuplicate

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