您的位置:首页 > 其它

dupicateII leetcode

2015-10-25 23:28 148 查看
import java.util.HashMap;

public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i = 0;i< nums.length;i++)
{
if(map.containsKey(nums[i])&&((i-map.get(nums[i])<=k)))
return true;
else
map.put(nums[i], i);
}
return false;
}
}

map是一个接口
而hashmap是它的一种实现方式

for  this problem, if you use eclipse,you will find that we need a () after the fucthion "new";

another should be pay attention is when I use eclipse as the edcitior and I found that map.get()'s para is key.

So we should put the array as <num[i],i>in the hash map we defined.

here is the code.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: