[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 ...
随机推荐
- Python分类统计数据
在数据的常见分布中,有一种是一对多存储的数据,即一个是key,其他改key对应的多个value.例如气象数据等,每天有很多组,又或者是一个球员,他每天得多少分等等.我做这个东西有三种方法,即:常规编程 ...
- 在/etc/password用户名前面加hello,ID前加is
方法2: #!/bin/sh #set -x file=/etc/passwd while read LINE #for i in `cat $file` do #username=`echo $i| ...
- jQuery的矿建结构小demo举例
(function (global) { var document = global.document,//变成局部变量提高搜索的性能 init;// 核心函数 function itcast(sel ...
- codevs 最佳落点(模拟)
/* 这题并没有A掉 自己电脑上运行ok提交就不对 预处理攻击范围 然后模拟 求大神看看有没有错误 Orz */ #include<iostream> #include<cstdio ...
- 2015-09-22CSS:border、background、表格、超链接、overflow、firebug
1.CSS的border属性 ⑴定义和用法 border 简写属性在一个声明设置所有的边框属性. 可以按顺序设置如下属性: border-width border-style border-color ...
- MySQL 删除数据库
MySQL 删除数据库 使用 mysqladmin 删除数据库 使用普通用户登陆mysql服务器,你可能需要特定的权限来创建或者删除 MySQL 数据库. 所以我们这边使用root用户登录,root用 ...
- QQ情侣头像~
- 使用wireshark抓本机之间的包(转)
所转地址:http://www.chinadmd.com/file/oc6evrwtzieitexvoupppisr_1.html 在进行通信开发的过程中,我们往往会把本机既作为客户端又作为服务器端来 ...
- 在fragment中获取他附着的activity中的变量
final String data=(关联的activity类)getActivity().getData(); getData();自定义的方法
- 《asp.net mvc3 高级编程》第三章 视图
一.视图的作用 视图的职责是向用户提供界面.从ASP.NET MVC3开始,视图数据也可以通过ViewBag属性访问.例如:ViewBag.Message 就等于ViewData["Mess ...