LeetCode 219--- Contains Duplicate II
2016-12-23 15:08
302 查看
题目链接:LeetCode 219— Contains Duplicate II
题目大意:判断给定数组中是否存在索引序号相差最大为k的两个相等的项
使用一个哈希表的思路即可轻松解决,实现代码如下:
题目大意:判断给定数组中是否存在索引序号相差最大为k的两个相等的项
使用一个哈希表的思路即可轻松解决,实现代码如下:
public class Problem219 { public static void main(String[] args) { // TODO Auto-generated method stub } public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap<Integer,Integer> hashtable = new HashMap<Integer,Integer>(); for(int i = 0; i < nums.length; i++){ if(hashtable.containsKey(nums[i]) && i - hashtable.get(nums[i]) <=k){ return true; } hashtable.put(nums[i],i); } return false; } }
相关文章推荐
- leetcode【217+219 Contains Duplicate I+II】【python】
- 【LeetCode-面试算法经典-Java实现】【219-Contains Duplicate II(包含重复元素II)】
- 【leetcode】219. Contains Duplicate II
- leetcode 219:Contains Duplicate II
- LeetCode *** 219. Contains Duplicate II
- leetcode219~Contains Duplicate II
- LeetCode219题:Contains Duplicate II
- LeetCode(219) Contains Duplicate II
- <easy>LeetCode Problem -- 219. Contains Duplicate II
- leetcode 219. Contains Duplicate II python
- leetcode 219. Contains Duplicate II
- LeetCode 219:Contains Duplicate II
- Leetcode题解 219. Contains Duplicate II
- LeetCode 219. Contains Duplicate II
- LeetCode(219) Contains Duplicate II
- 【leetcode】【219】Contains Duplicate II
- leetcode之219. Contains Duplicate II(C++解法)
- Contains Duplicate II - LeetCode 219
- leetcode 219: Contains Duplicate II
- LeetCode 219. Contains Duplicate II