[leedcode219]Contains Duplicate II
2015-05-31 15:02
405 查看
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 andj is at most k.
public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { if(nums==null || nums.length<2) return false; Map<Integer,Integer> map=new HashMap<Integer,Integer>(); for(int i=0;i<nums.length;i++){ if(map.containsKey(nums[i])){ int j=map.get(nums[i]); if(i-j<=k)return true; } map.put(nums[i],i);//****** } return false; } }
相关文章推荐
- SGU119 Magic Pairs
- Internal Sorting: Straight Two-way Merge Sort: Sorting by Merging
- 535 Error:authentication failed
- Searching: Chained Hash Table Search And Insertion-1
- Searching: Chained Hash Table Search And Insertion-2
- uvalive 6886 Flowery Trails fft
- LeetCode Contains Duplicate
- LeetCode Contains Duplicate II
- #172 Factorial Trailing Zeroes
- AI初级模型
- Skill: Add new mail attention extension in Thunderbird
- leetcode Contains Duplicate II
- leetcode Contains Duplicate
- IDR_MAINFRAME 字符串 MFC
- #LeetCode OJ #11 Container With Most Water
- 关于Error: xz compression not available
- Solving "Vietnam snake" puzzle with constraint logic programming
- 使用行为树(Behavior Tree)实现游戏AI
- Failed to find provider info for com.google.settings
- leetcode Factorial Trailing Zeroes