滑动窗口


  滑动窗口(sliding windows algorithm)这种方法,专门用于解决区间解的问题。它在运算的时候,将解集放在窗口中,结束的时候比对是否符合预期。在运算的过程中,会对窗口的左右边缘进行操作(扩大、缩小)。特别针对于线性输入解决,滑动窗口就很形象了。

30. Substring with Concatenation of All Words

  You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.

  题意是从一串输入字符串s中,找到能全包含words数组的起点(数组元素等长),这个起点可能有多个,而且不需要关心顺序。

Input:
s = "barfoothefoobarman",
words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoo" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.

  比如这个例子,他在第0个位置和第9个位置可以包含words数组,也就是0位置和9位置各有一个长度为6的窗口可以囊括words数组。

Template = "foo","bar"

i=

[b a r] f o o t h e f o o b a r m a n 

[b a r f o o] t h e f o o b a r m a n 

SAVE

b a r f o o[] t h e f o o b a r m a n 

b a r f o o [t h e] f o o b a r m a n 

b a r f o o t h e [f o o] b a r m a n 

b a r f o o t h e [f o o b a r] m a n 

SAVE

b a r f o o t h e f o o b a r[] m a n 

b a r f o o t h e f o o b a r [m a n] 

i=

b [a r f] o o t h e f o o b a r m a n
.... i= b a [r f o] o t h e f o o b a r m a n
....

  每次扩展使用单词列表中的词长,如果扩展过程中不符合预期则清除窗口,窗口左端在当前词总数大于词组总数的时候,计算总数是单词的长度(因为s除以单词长度余数是0到单词长度之间,覆盖了余数就能覆盖整个场景)。

public class FindSubstring {
public void test(){
// [0,9]
String s1 = "barfoothefoobarman";
String[] words1 = {"foo","bar"};
System.out.println(findSubstring(s1,words1));
// []
String s2 = "wordgoodgoodgoodbestword";
String[] words2 = {"word","good","best","word"};
System.out.println(findSubstring(s2,words2));
}
/**
* 单词等长
*/
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> result = new ArrayList<>();
if(s == null || words.length == 0){
return result;
}
int step = words[0].length();
Map<String,Integer> counter = new HashMap<>();
for(String word :words){
counter.merge(word, 1, (a, b) -> a + b);
} for(int i=0;i<step;++i){
Map<String,Integer> window = new HashMap<>();
int left = i;
int right = i;
while (right <= s.length() - step && left <= s.length() - step*words.length){
String sub = s.substring(right,right + step);
window.merge(sub,1,(a , b)->a + b);
if(!counter.containsKey(sub)){
window.clear();
right += step;
left = right;
continue;
}
while (window.get(sub) > counter.get(sub)){
String drop = s.substring(left,left+step);
Integer dec = window.get(drop);
if(dec != null){
if(dec<=1){
window.remove(drop);
}else {
window.put(drop,dec-1);
}
}
left += step;
}
right += step;
if(right - left == step * words.length){
result.add(left);
}
}
}
return result;
} }

76. Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

Example:

Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC" 题意是将s子串中包含T的所有字母的最短子串找出来,例子中BANC是包含ABC的最短子串。
Template = "ABC"

[]A D O B E C O D E B A N C
[A] D O B E C O D E B A N C
[A D] O B E C O D E B A N C
[A D O] B E C O D E B A N C
[A D O B] E C O D E B A N C
[A D O B E] C O D E B A N C
[A D O B E C] O D E B A N C
CONTAINS ABC mark substring A D O B E C
...
[A D O B E C O D E B] A N C
B now > so moving left but A is compliance
...
[A D O B E C O D E B A] N C
A > moving left
A D O [B E C O D E B A] N C
B > moving left
A D O B E [C O D E B A] N C
C O D E B A is not shorter than A D O B E C
...
A D O B E [C O D E B A N C]
C > moving left
A D O B E C O D E [B A N C]
B A N C is shorter than A D O B E C mark substring B A N C
import java.util.HashMap;
import java.util.Map; public class MinimumWindowSubstring {
public void test(){
String s1 = "ADOBECODEBANC";
String t1 = "ABC";
// BANC
System.out.println(minWindow(s1,t1)); String s2 = "a";
String t2 = "aa";
// ""
System.out.println(minWindow(s2,t2)); String s3 = "a";
String t3 = "b";
// ""
System.out.println(minWindow(s3,t3)); String s4 = "a";
String t4 = "a";
// a
System.out.println(minWindow(s4,t4)); String s5 = "ab";
String t5 = "b";
// b
System.out.println(minWindow(s5,t5)); String s6 = "aa";
String t6 = "aa";
// aa
System.out.println(minWindow(s6,t6)); String s7 = "acbbaca";
String t7 = "aba";
// baca
System.out.println(minWindow(s7,t7)); String s8 = "aaaaaaaaaaaabbbbbcdd";
String t8 = "abcdd"; System.out.println(minWindow(s8,t8)); }
public String minWindow(String s, String t) {
if(s == null || t == null || s.isEmpty() || t.isEmpty() || s.length() < t.length()){
return "";
}
Map<Character,Integer> counter = new HashMap<>();
char[] sArray = s.toCharArray();
for(char c :t.toCharArray()){
counter.merge(c,1,(a,b)->a+b);
}
int left = 0;
int right ;
int[] ans = {-1 , 0 , 0};
Map<Character,Integer> window = new HashMap<>();
for(int i=0;i<sArray.length;i++){
char c = sArray[i];
right = i;
if(counter.containsKey(c)){
window.merge(c,1,(a,b)->a+b);
if(window.keySet().size() == counter.keySet().size()){
while (true){
Integer dec = window.get(sArray[left]);
Integer standard = counter.get(sArray[left]);
if(dec != null && standard != null){
if(dec <= standard){
break;
}
if(dec > standard){
window.merge(sArray[left],-1,(a,b)->a+b);
}
}
left ++;
}
boolean valid = true;
for(char v : counter.keySet()){
if(window.get(v) < counter.get(v)){
valid = false;
}
}
// mark if initial or shorter sequence
if(valid && (ans[0] == -1 || right - left + 1 < ans[0])){
ans[0] = right - left + 1;
ans[1] = left;
ans[2] = right;
}
}
}
}
return ans[0] == -1?"":s.substring(ans[1],ans[2]+1);
}
}

239. Sliding Window Maximum

Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.

Input: nums = [,,-,-,,,,], and k =
Output: [,,,,,]
Explanation: Window position Max
--------------- -----
[ -] -
[ - -]
[- - ]
- [- ]
- - [ ]
- - [ ]
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
List<Integer> result = new ArrayList<>();
if(nums == null || nums.length == 0){
return new int[]{};
}
int left = 0;
int windowMax = Integer.MIN_VALUE;
for(int i=0;i<nums.length;i++){
if(i - left + 1 > k){
int rv = nums[i];
int currentMax = windowMax;
if(nums[left] == windowMax){
currentMax = nums[left+1];
for(int t = left+1 ; t <= i ; t ++){
currentMax = Math.max(nums[t],currentMax);
}
}
windowMax = Math.max(currentMax,rv);
result.add(windowMax);
left ++ ;
} else {
windowMax = Math.max(nums[i],windowMax);
if(i - left + 1 == k){
result.add(windowMax);
}
}
}
int[] copyRes = new int[result.size()];
for(int index = 0 ; index < result.size() ; index ++){
copyRes[index] = result.get(index);
}
return copyRes;
}
}
												

从一道Hard学习滑动窗口的更多相关文章

  1. 【转】20-TCP 协议(滑动窗口——基础)

    https://blog.csdn.net/q1007729991/article/details/70142341 相信大家都遇到过这样的场景: 同学 Luffy 给你打电话,让你记下一串手机号码, ...

  2. 计算机网络之流量控制(停止-等待协议、滑动窗口、后退N帧协议GBN、选择重传协议SR)、滑动窗口、可靠传输机制

    文章转自:https://blog.csdn.net/weixin_43914604/article/details/104908762 学习课程:<2019王道考研计算机网络> 学习目的 ...

  3. TCP协议总结--停止等待协议,连续ARQ协议,滑动窗口协议

    前言:在学习tcp三次握手的过程之中,由于一直无法解释tcpdump命令抓的包中seq和ack的含义,就将tcp协议往深入的了解了一下,了解到了几个协议,做一个小结. 先来看看我的问题: 这是用tcp ...

  4. [LeetCode] Sliding Window Median 滑动窗口中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  5. 第二十六节,滑动窗口和 Bounding Box 预测

    上节,我们学习了如何通过卷积网络实现滑动窗口对象检测算法,但效率很低.这节我们讲讲如何在卷积层上应用这个算法. 为了构建滑动窗口的卷积应用,首先要知道如何把神经网络的全连接层转化成卷积层.我们先讲解这 ...

  6. 一篇带你读懂TCP之“滑动窗口”协议

    前言 你现在的努力,是为了以后有更多的选择. 在上一篇文章通过"表白"方式,让我们快速了解网络七层协议了解了网络七层协议. 接下来我们要把重心放在网络传输的可靠性上面.一起来看TC ...

  7. 滑动窗口解决Substring Search Problem

    2018-07-18 11:19:19 一.Minimum Window Substring 问题描述: 问题求解: public String minWindow(String s, String ...

  8. [DeeplearningAI笔记]卷积神经网络3.1-3.5目标定位/特征点检测/目标检测/滑动窗口的卷积神经网络实现/YOLO算法

    4.3目标检测 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.1目标定位 对象定位localization和目标检测detection 判断图像中的对象是不是汽车--Image clas ...

  9. 单调队列优化&&P1886 滑动窗口题解

    单调队列: 顾名思义,就是队列中元素是单调的(单增或者单减). 在某些问题中能够优化复杂度. 在dp问题中,有一个专题动态规划的单调队列优化,以后会更新(现在还是太菜了不会). 在你看到类似于滑动定长 ...

随机推荐

  1. 一百一十:CMS系统之剩余菜单栏的页面和视图

    增加所有剩余菜单的页面,并用视图渲染,方便后面调试权限控制 {% extends 'cms/cms_base.html' %} {% block title %}板块管理{% endblock %} ...

  2. js大文件上传

    一般10M以下的文件上传通过设置Web.Config,再用VS自带的FileUpload控件就可以了,但是如果要上传100M甚至1G的文件就不能这样上传了.我这里分享一下我自己开发的一套大文件上传控件 ...

  3. JAVA 基础编程练习题1 【程序 1 不死神兔】

    1 [程序 1 不死神兔] 题目:古典问题:有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月 又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少? 程序分析: 兔子 ...

  4. springboot整合es客户端操作elasticsearch(二)

    在上章节中整合elasticsearch客户端出现版本问题进行了处理,这章来进行springboot整合得操作 环境:elaticsearch6.2.1,springboot 2.1.8 客户端版本采 ...

  5. Lua字符串及模式匹配

    字符类基础函数举例介绍: string.len( ‘string’ ) string.lower( ‘string’ ) string.upper( ‘string’ ) string.rep( ‘a ...

  6. [转帖]华为Mate20 X 5G版拆解:巴龙5000还配备了3GB独立内存!

    华为Mate20 X 5G版拆解:巴龙5000还配备了3GB独立内存! 投递人 itwriter 发布于 2019-07-29 21:35 评论(7) 有1733人阅读 原文链接 [收藏] « » h ...

  7. Git基本理解

    1.版本控制 Git 是一个分布式版本控制系统 (Distributed Version Control System - DVCS). 所谓版本控制,意思就是在文件的修改历程中保留修改历史,让你可以 ...

  8. 线段树维护最后一个0的位置(Restore Permutation)Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)

    题意:https://codeforc.es/contest/1208/problem/D 给你长度为n的序列,s[i]的值为p[1]到p[i-1]中比p[i]小的数的和,让你求出p序列. 思路: 首 ...

  9. raspberrypi 树莓派 内核编译

    相关版本信息 硬件:树莓派 2b 目标系统: linux 编译环境:ubuntu 14.4 32bit 用户路径:/home/hi/ 安装交叉编译链 cdmkdir pi/kernelcd pi/ke ...

  10. VirtualBox网络之仅主机(Host-Only)网络

    https://blog.csdn.net/dkfajsldfsdfsd/article/details/79441874