[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 ...
随机推荐
- linux安装tomcat(转载:http://blog.csdn.net/zhuihunmiling/article/details/8977387)
在安装Tomcat之前需要安装j2sdk(Java 2 Software Development Kit),也就是JDK 1.安装JDK完毕. 2.安装Tomcat 1)下载apache-tomcat ...
- Android滚动截屏,ScrollView截屏
在做分享功能的时候,需要截取全屏内容,一屏展示不完的内容,一般我们会用到 ListView 或 ScrollView 一: 普通截屏的实现 获取当前Window 的 DrawingCache 的方式, ...
- Linq101-Ordering
using System; using System.Collections.Generic; using System.Linq; namespace Linq101 { class Orderin ...
- 安装ZendDebugger 调试php
一直懒得装断点调试工具,平时调试就用echo var_dump debug_print_backtrace 搞搞. 今天同事装了个xdebug,看着眼馋.于是想自己也装一个,由于平时用zend stu ...
- Java序列化的机制和原理
Java序列化的机制和原理 本文讲解了Java序列化的机制和原理.从文中你可以了解如何序列化一个对象,什么时候需要序列化以及Java序列化的算法. 有关Java对象的序列化和反序列化也算是Java基础 ...
- Linux 网络配置(固定IP)
通常linux作为服务器系统时,是不推荐安装图形界面的,因此我们需要掌握非图形界面下如何配置网络,主要两种方式,如下: 一.使用SETUP工具(redhat系列才可以,推荐此修改方式) 1.在命令行直 ...
- java Enum 用法示例
public enum MyEnum { Monday, Tuesday, Wednesday, Thursady, Friday, Saturday, Sunday; public static v ...
- Maven Profile标签
Maven Profiles标签可以针对不同的环境来使用不同的配置文件 在发布的时候可以用 mvn release -p product mvn release -p test mvn release ...
- C++ 实现网络爬虫
吐槽 前天心血来潮, 把自己面试经历下了下来. 我觉得自己求职一路来比较心酸, 也付出了比一般人更多的汗水. 本以为写出来, 好歹可以作为一篇励志故事. 得到的评论却是, 语言只是一门工具. ||| ...
- 用core dump来调试程序段错误
有的程序可以通过编译, 但在运行时会出现Segment fault(段错误). 这通常都是指针错误引起的.但这不像编译错误一样会提示到文件->行, 而是没有任何信息, 使得我们的调试变得困难起来 ...