LeetCode题解:Contains Duplicate II
2015-08-12 11:11
387 查看
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.
题意:给定数组和整数k,找出数组内相同元素,且两个元素距离小于等于k
解决思路:和前一个思路一样,只不过多加一个判断
代码:
题意:给定数组和整数k,找出数组内相同元素,且两个元素距离小于等于k
解决思路:和前一个思路一样,只不过多加一个判断
代码:
public boolean containsNearbyDuplicate(int[] nums, int k) { Set<Integer> set = new HashSet<Integer>(); int i = 0, j = 0; for (; j < nums.length; j++) { if (j - i > k) { set.remove(nums[i]); i++; } if (set.contains(nums[j])) return true; set.add(nums[j]); } return false; }
相关文章推荐
- CoreData之条件查询 contains
- LeetCode题解:Contains Duplicate
- Dynamics CRM 开启EmailRouter日志记录
- Dynamics CRM 开启EmailRouter日志记录
- Dynamics CRM 开启EmailRouter日志记录
- Dynamics CRM 开启EmailRouter日志记录
- 再谈应用环境下的TIME_WAIT和CLOSE_WAIT
- 新型程序设计语言:Braise语言!
- HDU 3394 双连通分量 桥 Railway
- Contains Duplicate III
- 解决Failed to allocate memory: 8
- 2015 Multi-University Training Contest 7 hdu 5371 Hotaru's problem
- 如何获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)
- 报错:failed to get the task for process XXX(解决方案)
- 一个epoll/aio/eventfd结合使用的简单例子
- 数据挖掘十大算法——Naive Bayes
- Failed to load class "org.slf4j.impl.StaticLoggerBinder问题故障的解决
- HDU 2473 Junk-Mail Filter (并查集的删除操作)
- scala 学习笔记(05) OOP(中)灵活的trait
- 2015 Multi-University Training Contest 7(hdu5371、5372、5373、5375、5378、5379)(dp+概率Manachar+dfs计数+树状数组)