Leetcode - Contains Duplicate II
来源:互联网 发布:dota2公开比赛数据 编辑:程序博客网 时间:2024/05/29 07:32
Question
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.
Java Code
public boolean containsNearbyDuplicate(int[] nums, int k) { int len = nums.length; //使用HashMap容器存储已经遍历过的数组元素及其索引 HashMap<Integer, Integer> map = new HashMap<>(2*len); int num; Integer temp; for(int i = 0 ; i < len; ++i) { num = nums[i]; //如果map中不存在此元素或其索引与当前元素的索引之差超过k,则存入/更新该元素及其索引 if((temp = map.get(num)) == null || temp < i - k) map.put(num, i); else//如果map中已经存在此元素,且其索引与当前元素的索引之差不超过k,则满足条件 return true; } return false;}
0 0
- [LeetCode]Contains Duplicate II
- [LeetCode] Contains Duplicate II
- leetcode Contains Duplicate II
- LeetCode Contains Duplicate II
- [leetcode] Contains Duplicate II
- LeetCode Contains Duplicate II
- 【leetcode】Contains Duplicate II
- leetcode--Contains Duplicate II
- [leetcode]Contains Duplicate II
- [leetCode] Contains Duplicate II
- LeetCode: Contains Duplicate II
- LeetCode Contains Duplicate II
- LeetCode | Contains Duplicate II
- LeetCode Contains Duplicate II
- 【LeetCode】Contains Duplicate II
- leetcode: Contains Duplicate II
- LeetCode Contains Duplicate II
- Leetcode: Contains Duplicate II
- 第4课版本定制:Spark Streaming事务处理彻底掌握
- Android开发实现发送短信验证码(使用第三方服务器Bmob)
- Xcode7.3.1中SKAudioNode在Scene转换后无声的问题
- word2013 图片与表格自动更新编号
- C# socket连接断开问题
- Leetcode - Contains Duplicate II
- 这速度 也是醉了
- 如何在调试Window App时,触发 Suspending ,Resuming 等事件
- POJ 1741Tree
- CentOS6.5环境使用keepalived实现nginx服务的高可用性及配置详解
- 在二维数组中查找一个数
- Leetcode 147. Insertion Sort List
- 242. Valid Anagram
- More Effective C++----(6)自增、自减操作符前缀形式和后缀形式的区别