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.
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.
相关文章推荐
- day1 python学习随笔
- 《探索需求》读书笔记二
- Response详解
- 什么是MySql数据库
- 《探索需求》读书笔记二
- 上善若水,虚怀若谷
- Axis2用法:客户端和服务端
- 114_面向对象(多异常处理)_黑马程序员_Java基础视频
- effective c++在资源管理类中提供对原始资源的访问
- Request和ServletConfig
- 通过拼音来搜索对应的汉字字段
- 状态模式
- Python获取并修改hosts
- 越努力越幸运
- 【计算机系统知识】校验码和数据表示
- 113_面向对象(异常声明throws)_黑马程序员_Java基础视频
- Servlet详解
- Android Volley完全解析(一),初识Volley的基本用法
- Android 获取AndroidManifest.xml 中 meta-data 的值
- Java实现定时任务的3种方式