LeetCode 滑动窗口题型整理
一、滑动窗口题型模板
/*
* 滑动窗口类型: 模板
*/
public List<Integer> slideWindowMode(String s, String t) {
// 1 根据题目返回类型定义数据结构
ArrayList<Integer> result = new ArrayList<>();
if(t.length()> s.length())
return result;
// 2 新建Map, Key=字符,value=频率
HashMap<Character, Integer> map = new HashMap<>();
for(char c: t.toCharArray())
map.put(c, map.getOrDefault(c, 0) + 1); // 3 定义变量
int counter = map.size(); // 目标字符串中的不同字符种类
int begin = 0, end = 0; // 窗口起始、结束点
int len = Integer.MAX_VALUE; // 4 源字符串开始遍历
while(end < s.length()) {
// 5 统计每个字符
char c = s.charAt(end);
if(map.containsKey(c)) {
map.put(c, map.get(c) - 1);
if(map.get(c) == 0)
counter--;
}
end++; // 6 符合情况!begin~end 之间包含 t 的所有字符
while(counter == 0) { // 此时 Map 的 value 全部 <= 0
char tmpc = s.charAt(begin);
if(map.containsKey(tmpc)) {
map.put(tmpc, map.get(tmpc) + 1);
if(map.get(tmpc) > 0)
counter++;
} // 7 do sth begin++;
}
}
// 8
return result;
}
二、LeetCode 中 几个滑动窗口套用模板实现
1、 76. Minimum Window Substring
https://leetcode.com/problems/minimum-window-substring/description/
public String minWindow(String s, String t) {
if(t.length()> s.length()) return "";
HashMap<Character, Integer> map = new HashMap<>();
for(char c: t.toCharArray())
map.put(c, map.getOrDefault(c, 0) + 1);
int counter = map.size(); // 目标字符串中的不同字符种类
int begin = 0, end = 0; // 窗口起始、结束点
int len = Integer.MAX_VALUE;
int head = 0;
while(end < s.length()) {
char c = s.charAt(end);
if( map.containsKey(c) ) {
map.put(c, map.get(c) - 1);
if(map.get(c) == 0)
counter--;
}
end++;
while(counter == 0) { // 此时 Map 的 value 全部 <= 0
char tmpc = s.charAt(begin);
if( map.containsKey(tmpc) ) {
map.put(tmpc, map.get(tmpc) + 1);
if(map.get(tmpc) > 0)
counter++;
}
//----------
if(end - begin < len) {
len = end - begin;
head = begin;
}
//----------
begin++;
}
}
if(len == Integer.MAX_VALUE )
return "";
return s.substring(head, head + len);
}
2、3 Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
/*
* 2、 Longest Substring Without Repeating Characters
*/
public int lengthOfLongestSubstring(String s) { HashMap<Character, Integer> map = new HashMap<>();
int begin = 0; // 窗口起始、结束点
int max = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(map.containsKey(c)) {
max = Math.max(max, i - begin);
begin = Math.max(begin, map.get(c) + 1);
map.put(c, i);
}
else {
map.put(c, i);
}
}
max = Math.max(s.length() - begin, max);
return max;
}
3、438. Find All Anagrams in a String
https://leetcode.com/problems/find-all-anagrams-in-a-string/description/
public List<Integer> findAnagrams(String s, String t) {
ArrayList<Integer> result = new ArrayList<>();
if(s.length() < t.length())
return result;
HashMap<Character, Integer> map = new HashMap<>();
for(char c: t.toCharArray())
map.put(c, map.getOrDefault(c, 0) + 1);
int counter = map.size();
int begin = 0, end = 0;
// int head = 0;
// int len = Integer.MAX_VALUE;
while(end < s.length()) {
char c = s.charAt(end);
if( map.containsKey(c) ) {
map.put(c, map.get(c) - 1);
if(map.get(c) == 0)
counter--;
}
end++;
while(counter == 0) {
char tmpc = s.charAt(begin);
if( map.containsKey(tmpc) ) {
map.put(tmpc, map.get(tmpc) + 1);
if(map.get(tmpc) > 0)
counter++;
}
//--------
if(end - begin == t.length())
result.add(begin);
//--------
begin++;
}
}
return result;
}
LeetCode 滑动窗口题型整理的更多相关文章
- LeetCode - 滑动窗口最大值
给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右移动一位. 返回滑动窗口中的最大值. 输入: nums ...
- 【Leetcode 滑动窗口】顺次数(1291)
题目 我们定义「顺次数」为:每一位上的数字都比前一位上的数字大 1 的整数. 请你返回由 [low, high] 范围内所有顺次数组成的 有序 列表(从小到大排序). 示例 1: 输出:low = ...
- 端口状态 LISTENING、ESTABLISHED、TIME_WAIT及CLOSE_WAIT详解,以及三次握手四次挥手,滑动窗口(整理转发)
网上查了一下端口状态的资料,我下面总结了一下,自己学习学习: TCP状态转移要点 TCP协议规定,对于已经建立的连接,网络双方要进行四次握手才能成功断开连接,如果缺少了其中某个步骤,将会使连接处于假死 ...
- [LeetCode] Sliding Window Maximum 滑动窗口最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- [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 ...
- LeetCode编程训练 - 滑动窗口(Sliding Window)
滑动窗口基础 滑动窗口常用来解决求字符串子串问题,借助map和计数器,其能在O(n)时间复杂度求子串问题.滑动窗口和双指针(Two pointers)有些类似,可以理解为往同一个方向走的双指针.常用滑 ...
- leetcode的Hot100系列--3. 无重复字符的最长子串--滑动窗口
可以先想下这两个问题: 1.怎样使用滑动窗口? 2.如何快速的解决字符查重问题? 滑动窗口 可以想象一下有两个指针,一个叫begin,一个叫now 这两个指针就指定了当前正在比较无重复的字符串,当再往 ...
- [LeetCode] 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 a ...
- 【LeetCode】无重复字符的最长子串【滑动窗口法】
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc&quo ...
随机推荐
- Access restriction: The type JPEGImageEncoder is not accessible due to restriction
转: 解决办法:Access restriction: The type JPEGImageEncoder is not accessible due to restriction 2011年11月2 ...
- Java Web之过滤器(Filter)
转: Java Web之过滤器(Filter) 2018年07月31日 16:58:40 喻志强 阅读数 13705更多 所属专栏: Java Web入门 版权声明:本文为博主原创文章, 转载请注 ...
- [spring]数据库的连接配置
使用druid数据源 ,并支持事务处理. <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...
- 图解 HTTP 笔记(六)——HTTP 首部
本章主要讲解了 HTTP 首部的结构,已经首部中各字段的用法. 一.HTTP 报文首部 上图是 HTTP 请求报文的结构. HTTP 请求报文由方法.URI.HTTP 版本.HTTP 首部字段等组成. ...
- 电力项目十一--js添加浮动框
1.添加浮动窗口样式 <!-- 浮动窗口样式css begin --> <style type="text/css"> #msg_win{border:1p ...
- Spring Aop(十三)——ProxyFactoryBean创建代理对象
转发地址:https://www.iteye.com/blog/elim-2398673 ProxyFactoryBean创建代理对象 ProxyFactoryBean实现了Spring的Factor ...
- 为什么要设置 Mysql 的 ft_min_word_len=1
为什么要设置 Mysql 的 ft_min_word_len=1 ? 从 Mysql 4.0 开始就支持全文索引功能,但是 Mysql 默认的最小索引长度是 4.如果是英文默认值是比较合理的,但是中文 ...
- centos7 vim环境优化
centos7默认是使用vi,而不是使用vim,所以,我们需要修改一下vi的别名,并且,我们使用neovim,vi毕竟还是有很多功能比较原始 所以 yum install neovim -ycat & ...
- 安装mysqlmysql-5.7.24-linux-glibc2.12-x86_64
1.下载mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz(/opt目录) 2.tar -zxvf mysql-5.7.24-linux-glibc2.12-x86_ ...
- Centos7下 升级php5.4到7.1 yum安装
1.查看当前 PHP 版本 php -v 查看当前 PHP 相关的安装包,删除之 yum list installed | grep php yum remove php yum remove php ...