您的位置:首页 > 大数据 > 人工智能

[leetCode] Contains Duplicate II

2015-06-18 22:28 351 查看
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.

public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Hashtable <Integer, List<Integer>> table = new Hashtable<Integer, List<Integer>>();

for (int i = 0; i < nums.length; i++) {
if (table.containsKey(nums[i])) {
List<Integer> tmp = table.get(nums[i]);

for (int j = 0; j < tmp.size(); j++) {
if (tmp.get(j) + k >= i) return true;
}
tmp.add(i);
}
else {
List<Integer> tmp = new ArrayList<Integer>();
tmp.add(i);
table.put(nums[i], tmp);
}
}
return false;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Array leetcode Hash Table