Contains Duplicate II
来源:互联网 发布:网络作家真实故事 编辑:程序博客网 时间:2024/06/10 00:44
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.
思路:用hashmap来用空间换时间,就是算每个出现的index跟前面的index距离是否超过k,如果 没有,return true,如果超过,覆盖index,继续往下走。
public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { if(nums == null || nums.length == 0 || k<=0) return false; HashMap<Integer,Integer> hashmap = new HashMap<Integer, Integer>(); for(int i=0; i<nums.length; i++){ if(hashmap.containsKey(nums[i])){ if(i - hashmap.get(nums[i]) <=k){ return true; }else{ hashmap.put(nums[i],i); } } else { hashmap.put(nums[i],i); } } return false; }}
0 0
- Contains Duplicate && Contains Duplicate II
- [LeetCode]Contains Duplicate II
- Contains Duplicate II
- [LeetCode] Contains Duplicate II
- leetcode Contains Duplicate II
- LeetCode Contains Duplicate II
- [leetcode] Contains Duplicate II
- LeetCode Contains Duplicate II
- LeetCode219:Contains Duplicate II
- #219 Contains Duplicate II
- 219. Contains Duplicate II
- 【leetcode】Contains Duplicate II
- leetcode--Contains Duplicate II
- 219Contains Duplicate II
- Contains Duplicate II
- Contains Duplicate II
- Contains Duplicate II
- Leetcode_219 Contains Duplicate II
- Eclipse 快捷键手册
- socket编程的select模型
- IP、TCP、UDP数据包长度问题
- __block 与 __weak的区别与使用
- 天长地久,人生几何,先后无觉,从尔有期
- Contains Duplicate II
- 提示的函数名称是alert,总是记不住~~
- vimrc易读精简版(100ms启动)
- [LeetCode] 1. Two Sum
- 详细对比C语言中的chmod()函数和fchmod()函数
- 321. Create Maximum Number
- 关于C语言标准的一些总结
- 古诗十九首
- [2016/7/11][usaco 2.1][欧几里得算法]Ordered Fractions