Contains Duplicate II
给一个数组和一个数字k, 问数组有没有两个数想问, 但是index的差为最多k.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i = 0; i < nums.length; i++) { Integer ord = map.put(nums[i], i); if(ord != null && i - ord <= k) { return true; } } return false; } } |