Leetcode: Contains Duplicate II
2015-12-17 07:44
495 查看
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.
Better idea is to maintain a hashset that contains at most k numbers
public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { Set<Integer> set = new HashSet<Integer>(); for(int i = 0; i < nums.length; i++){ if(set.contains(nums[i])) return true; set.add(nums[i]); if(set.size()>k) set.remove(nums[i-k]); } return false; } }
相关文章推荐
- Leetcode: Contains Duplicate
- http://blog.csdn.net/linjingj/article/details/18881755
- 将PPT文件内容转换为图片放在Email邮件正文中发送
- LeetCode 172:Factorial Trailing Zeroes
- main函数的三个入参
- Daily Scrum NO.6
- printf的重入问题及解决方法 http://blog.csdn.net/iefswang/article/details/7523458
- Daily Scrumming* 2015.12.16(Day 8)
- Hotspot & Airplane mode
- <LeetCode OJ> 70. Climbing Stairs
- TypeError: 'module' object is not callable cp fromhttp://blog.csdn.net/huang9012/article/details/17417133
- 删除本地过期图pain
- hdu 3980 Paint Chain sg函数
- iTunes store operation failed: You are not authoried to use this service
- explain extend
- 偶尔发生File has been moved - cannot be read again
- 使用lingpipe自然语言处理包进行文本分类/** * 使用 lingpipe的tf/idf分类器训练语料 * * @author laigood */ public class trai
- Unix域套接字(Unix Domain Socket)介绍
- v$session_wait中参数
- [转载]offsetof与container_of宏