[LeetCode] 219. Contains Duplicate 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 j is at most k.
问题:给定一个数组和整数 k ,判断是否存在两个相等元素,并且两个相等元素的下标差在 k 以内?
问题问是否存在一个子数组满足某种条件,首先想到的是滑动窗口(sliding window)的模型。
将 [0, k] 视为一个窗口,将窗口向右滑动直到滑到最右端,期间判断窗口中是否存在相等元素。只要其中一个窗口存在相等元素,即原题目存在相等元素,否则,不存在。
判断一个数是否存在于一个集合中,可以用 hash_table ,即 c++ 中的 unordered_map。
本题目的思路使用的是长度固定的滑动窗口,其他同样可以理解为滑动窗口的还有:
Search Insert Position,每次长度减半的滑动窗口
Minimum Size Subarray Sum , 求最大/最小连续子数组的滑动窗口。窗口长度无规律。
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if(k == ){
return false;
}
unordered_map<int, int> int_cnt;
for(int i = ; i <= k && i < nums.size() ; i++){
if(int_cnt.count(nums[i]) == ){
int_cnt[nums[i]] = ;
}else{
return true;
}
}
for (int i = ; (i + k) < nums.size(); i++){
int_cnt.erase(nums[i-]);
if(int_cnt.count(nums[i+k]) == ){
int_cnt[nums[i+k]] = ;
}else{
return true;
}
}
return false;
}
[LeetCode] 219. Contains Duplicate II 解题思路的更多相关文章
- [LeetCode] 45. Jump Game II 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Java for LeetCode 219 Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i ...
- Java [Leetcode 219]Contains Duplicate II
题目描述: Given an array of integers and an integer k, find out whether there are two distinct indices i ...
- [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] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
- 【LeetCode】219. Contains Duplicate II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用set 使用字典 日期 题目地址:https:/ ...
- LeetCode 219. Contains Duplicate II (包含重复项之二)
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- LeetCode 219 Contains Duplicate II
Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...
- Leetcode 219 Contains Duplicate II STL
找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...
随机推荐
- java.util.Date和java.sql.Date
java.util.Date是在除了SQL语句的情况下面使用的. java.sql.Date是针对SQL语句使用的,它只包含日期而没有时间部分 它们都有getTime方法返回毫秒数,自然就可以直接构建 ...
- sublime 3 3083验证码
Sublime Text 3注册码两枚: ----- BEGIN LICENSE ----- K- Single User License EA7E- 3A099EC1 C0B5C7C5 33EBF0 ...
- MediaCodec文档翻译
MediaCodec|文档翻译 classoverView mediacodec类可以用来调用系统底层的编码/解码软件. mediacodec一般是这么用的: MediaCodec codec = M ...
- 移动端屏幕自适应js与rem
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;&qu ...
- Dhroid框架笔记(IOC、EventBus)
dhroid 目前包含了6大组件供大家使用1.Ioc容器: (用过spring的都知道)视图注入,对象注入,接口注入,解决类依赖关系2.Eventbus: android平台事件总线框架,独创延时事件 ...
- react服务端渲染(同构)
学习react也有一段时间了,使用react后首页渲染的速度与seo一直不理想.打算研究一下react神奇服务端渲染. react服务端渲染只能使用nodejs做服务端语言实现前后端同构,在后台对re ...
- 小波 mallat 算法
算法要求:输入序列是大于滤波器长度的偶数列 确实可以通过编程的手段使算法适合所有的情况,但本文章的目的是展示mallat算法的过程,所以就一切从简了 // Mallat.cpp : Defines t ...
- 当OOP语言RAII特性发展到functional形式的极致
本文主要站在C++程序员的思维角度思量. functional之路 lambda表达式 lambda表达式,是一段代码片段.函数实现体中出现的可重用的代码块. 在C++之前,C语言最小可复用流程模块, ...
- 人见人爱a*b 杭电2035
求A^B的最后三位数表示的整数.说明:A^B的含义是“A的B次方” Input 输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A= ...
- Watchcow
传送门 题目大意: 给你一幅连通的图,要求从起点1开始走,要经过每条边刚好两次,并且最终回到1起点. 思路:将无向图转换成有向图求欧拉回路. #include<cstdio> #defin ...