LeetCode OJ:Contains DuplicateII(是否包含重复II)
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 jis at most k.
这题上上一篇博客的延伸,问的是k长的距离内有没有两个数是相等的,类似一个滑动窗口问题,方法比较简单,使用一个map记下上次数出现的位置就可以了,代码如下:
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
map<int, int> ret;
int sz = nums.size();
for (int i = ; i < sz; ++i){
if (ret.find(nums[i]) != ret.end() && i - ret[nums[i]] <= k)
return true;
else
ret[nums[i]] = i;
}
return false;
}
};
java版本的代码如下所示,用的方法都是一样的:
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
for(int i = 0; i < nums.length; ++i){
if(m.containsKey(nums[i]))
if(i-m.get(nums[i]) <= k)
return true;
m.put(nums[i], i);//放在这里有两个原因,如果本来存在将index更新到最近的位置,如果不存在就将它放到map中起
}
return false;
}
}
LeetCode OJ:Contains DuplicateII(是否包含重复II)的更多相关文章
- [LeetCode] 220. Contains Duplicate III 包含重复元素 III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- LeetCode 217. Contains Duplicate (包含重复项)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- LeetCode OJ 95. Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- <LeetCode OJ> 78 / 90 Subsets (I / II)
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- LeetCode OJ:Search a 2D Matrix II(搜寻二维矩阵)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)
题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...
- [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 ...
- [LeetCode] 217. Contains Duplicate 包含重复元素
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
随机推荐
- 0404-服务注册与发现-客户端负载均衡-两种自定义方式-Ribbon通过代码自定义配置、使用配置文件自定义Ribbon Client
一.官方文档解读 官方地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_cust ...
- MapX小试
需MapX 控件 string layerName = "12Q3_new"; string tabFile = string.Format(@"E:\map\地图\现在 ...
- LeetCode_Easy_471:Number Complement
LeetCode_Easy_471:Number Complement 题目描述 Given a positive integer, output its complement number. The ...
- 前端 css续
CSS选择器 1.标签选择器 为类型标签设置样式例如:<div>.<a>.等标签设置一个样式,代码如下: <style> /*标签选择器,找到所有的标签应用以下样式 ...
- linux mint —— 图片一张
概述 Linux Mint是一種基於Ubuntu開發出的Linux操作系统.由Linux Mint Team团队于2006年开始发行.Linux Mint 的目标是为家庭用户和企业客户提供一个免费.高 ...
- HttpServlet---getLastModified与缓存
在HttpServlet中重写service方法的代码如下: protected void service(HttpServletRequest req, HttpServletResponse re ...
- time_t、pthread_t
1.time_t实际上就是长整型long int;用来保存从1970年1月1日0时0分0秒到现在时刻的秒数!用time()这个函数获取! #ifndef __TIME_T #define __TIME ...
- Qt开发动画
#include <QPropertyAnimation> #include <QDesktopWidget> //下坠 void MainWindow::on_pushBut ...
- Service Meth and SideCar
本文转自:http://philcalcado.com/2017/08/03/pattern_service_mesh.html SideCar: SideCar就是与Application一起运行的 ...
- JMeter学习(八)JDBC测试计划-连接Oracle
一.测试环境准备 Oracle:10g JDBC驱动:classes12.jar oracle安装目录下(oracle\product\10.2.0\db_1\jdbc\lib\classes1 ...