Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]
题目:
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.
翻译:
给定一个整数数组和一个整数k。找出是否存在下标i,j使得nums[i] = nums[j]。同一时候i,j的差值小于等于k。
分析:
遍历数组。使用map存储每一个值近期一次出现的下标。
代码:
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer,Integer> map=new HashMap<>();
for(int i=0;i<nums.length;i++){
Integer value=map.get(nums[i]);
if(value!=null&&i-value<=k){
return true;
}else{
map.put(nums[i],i);
}
}
return false;
}
}
Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]的更多相关文章
- Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 思路分析: 题意是将二叉树全部左右子数 ...
- 219. Contains Duplicate II【easy】
219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...
- 219. Contains Duplicate II - LeetCode
Question 219. Contains Duplicate II Solution 题目大意:数组中两个相同元素的坐标之差小于给定的k,返回true,否则返回false 思路:用一个map记录每 ...
- [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
- Leet Code OJ 237. Delete Node in a Linked List [Difficulty: Easy]
题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...
- Leet Code OJ 338. Counting Bits [Difficulty: Medium]
题目: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate ...
- Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- [刷题] 219 Contains Duplicate II
要求 给出整型数组nums和整数k,是否存在索引i和j,nums[i]==nums[j],且i和j之间的差不超过k 思路 暴力解法(n2) 建立最长为k+1的滑动窗口,用set查找窗口中是否有重复元素 ...
随机推荐
- luogu P1011 车站
题目描述 火车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达第2站,在第2站有人上.下车,但上.下车的人数相同,因此在第2站开出时(即在到达第3站之前)车上的人数保持为a人.从第3站起( ...
- [BZOJ4556][TJOI2016&&HEOI2016]字符串(二分答案+后缀数组+RMQ+主席树)
4556: [Tjoi2016&Heoi2016]字符串 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1360 Solved: 545[S ...
- redis实现简单延时队列(转)
继之前用rabbitMQ实现延时队列,Redis由于其自身的Zset数据结构,也同样可以实现延时的操作 Zset本质就是Set结构上加了个排序的功能,除了添加数据value之外,还提供另一属性scor ...
- [转]怎么把一个textview的背景图片设置成圆角的?
在drawable文件夹下新建一个文件设置背景样式代码:在drawable文件夹下面新建text_view_border.xml<?xml version="1.0" ...
- Codeforces Round #301 (Div. 2) A. Combination Lock 暴力
A. Combination Lock Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/p ...
- 谨慎Asp.ne B/S架构t中static变量
在.Net平台下进行CS软件开发时,我们经常遇到以后还要用到某些变量上次修改后的值,为了简单起见,很多人都习惯用static来定义这些变量,我也是.这样非常方便,下一次调用某个函数时该变量仍然保存的是 ...
- 更新yum源/apt-get源
国内开源镜像站有:网易: http://mirrors.163.com/ 搜狐: http://mirrors.sohu.com/阿里云: http://mirrors.aliyun.com/北京理工 ...
- PHP session过期机制和配置
问题:使用PHP session时会遇到明明超过了session过期时间,但session依然完好无损的活着,让人头大. 其实仔细看一下php.ini关于PHP session回收机制就一目了然了. ...
- JAVA EE 博客实例
http://www.cnblogs.com/hoojo/category/276244.html
- mysql 虚拟列
http://blog.csdn.net/yueliangdao0608/article/category/351407