219. Contains Duplicate II
来源:互联网 发布:软件设计方案作用 编辑:程序博客网 时间:2024/06/07 14:55
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 absolute difference between i and j is at most k.
My solution:HashMap
public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { Map<Integer,Integer> data=new HashMap<Integer,Integer>(); for(int i=0;i<nums.length;++i){ if(data.containsKey(nums[i])) if(i-data.get(nums[i])<=k) return true; data.put(nums[i],i); } return false; }}Solution 2: 66666 in Discuss
public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { Set<Integer> data=new HashSet<Integer>(); for(int i=0;i<nums.length;++i){ if(i>k) data.remove(nums[i-k-1]); if(!data.add(nums[i])) return true; } return false; }}
0 0
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 你应该知道的setTimeout秘密
- 驾驭LoadRunner
- Android多媒体技术(一)Camera实时视频采集预览、拍照、JPEG图片方向的处理
- Java项目总结
- SharePoint 2016 自定义城市和区域字段
- 219. Contains Duplicate II
- Linux常用命令
- JAVA NIO 实例
- yum 安装卸载mysql
- Helloworld
- PAT乙级1033
- 如何用Maven把依赖也打到可自动执行的jar里面?
- 简单的qml抽奖demo
- css编写规范