219. Contains Duplicate II
2016-08-23 17:50
239 查看
Given an array of integers and an integer k, find out whether there are two distinct indicesi andj in the array such thatnums[i] = nums[j] and the difference betweeni andj is at mostk.
public boolean containsNearbyDuplicate(int[] nums, int k) { if (nums == null || nums.length <= 1 || k <= 0) return false; Map map = new HashMap(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(nums[i])) { int preIndex = (int) map.get(nums[i]); if (i - preIndex <= k) return true; } map.put(nums[i], i); } return false; }
相关文章推荐
- Leetcode 219. Contains Duplicate II
- 【leetcode】219. Contains Duplicate II
- [leetcode]219. Contains Duplicate II
- 219. Contains Duplicate II
- [LeetCode]--219. Contains Duplicate II
- 算法作业HW29: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
- Easy-题目41:219. Contains Duplicate II
- leetcode_c++:Contains Duplicate II(219)
- 219. Contains Duplicate II
- LeetCode OJ 系列之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