LeetCode:Contains Duplicate II
2016-01-24 22:12
393 查看
Contains Duplicate II
Total Accepted: 42592 TotalSubmissions: 148608 Difficulty: Easy
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 jis at most k.
Subscribe to see which companies asked this question
Hide Tags
Array Hash
Table
Hide Similar Problems
(E) Contains Duplicate (M)
Contains Duplicate III
code:
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(i>k) set.remove(nums[i-k-1]); if(!set.add(nums[i])) return true; } return false; } }
相关文章推荐
- A Large-Scale Car Dataset for Fine-Grained Categorization and Verification论文笔记
- LeetCode11. Container With Most Water最容易理解的解法
- codeforces 616E Sum of Remainders (数论,找规律)
- Fzu2109 Mountain Number 数位dp
- hdu 1039 Easier Done Than Said?
- wait3,wait4 用法
- Fragments的生命周期 (转载:http://blog.csdn.net/t12x3456/article/details/8104531)
- Ubuntu 15.04 Rails4.2.5 处理异常
- SSIS: RetainSameConnection 属性。
- 深度学习发展简史-推动人工智能的梦想
- 正则表达式(一个字符串翻转的例子)(http://www.cnblogs.com/hai98)
- 正则表达式基础(http://www.cnblogs.com/hai98)
- RAID技术介绍和总结
- 如何画人脑?
- LightOJ 1076 - Get the Containers
- DP------Climbing Stairs
- HDU 1039 Easier Done Than Said?
- YTU 1011: Rails
- 深度理解链式前向星 http://blog.csdn.net/acdreamers/article/details/16902023
- teradata EXPLAIN 读懂关键字--读懂执行计划