开始刷leetcode day63:Contains Duplicate II
2015-07-13 05:28
381 查看
Given an array of integers and an integer k,
find out whether there there are two distinct indices i and j in
the array such that nums[i] = nums[j] and
the difference between iand j is
at most k.
Java:
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
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); //replace the old j value
}else
{
hashmap.put(nums[i],i);
}
}
return false;
}
}
find out whether there there are two distinct indices i and j in
the array such that nums[i] = nums[j] and
the difference between iand j is
at most k.
Java:
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
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); //replace the old j value
}else
{
hashmap.put(nums[i],i);
}
}
return false;
}
}
相关文章推荐
- 开始刷leetcode day63: Contains Duplicate
- mail -s发送附件
- mail -s发送附件
- static--main--单例
- 2015Summer Training #2
- 配置SSH时遇到 "Host key verification failed." 的解决方案
- lightoj 1021 Painful Bases (状压dp)
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决办法
- 启动hive报错[ERROR] Terminal initialization failed; falling back to unsupported
- Air Raid(最小路径覆盖)
- Debug Assertion Failed问题解决
- 带参数main函数
- win7下cmake编译opencv2.3.1生成opencv—createsamples.exe和opencv_haartrainingd.exe
- openCV中waitKey函数介绍
- poj 1273 Drainage Ditches(最大流入门)
- Climbing Stairs
- hdu 1789 Doing Homework again(贪心)
- HDU 1532 Drainage Ditches 排水渠(最大流,入门)
- See you again(电影《速度与激情7》主题曲)
- TJU-3980Painting Tree(贪心)