当前位置:网站首页>每日一题-滑动窗口
每日一题-滑动窗口
2022-08-11 03:53:00 【菜鸡程序媛】
目录
最小覆盖子串
- 时间:0810
- 题目
给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。
- 思路
- 剪枝:当s的长度小于t的时候,就凑不齐t的所有字符
- 找到s中包含t中全部字母的最小子串,这个就用到了将字母字典作为下标的思路:
- 初始化一个数组,数组下标就是字母,字母在t中出现过++
- 再遍历s,遇到一个字母,就对应整型数组的值就,当遇到减完之后还大等于0的,就证明在t中出现过,否则减完就是负数拉
- 当记录的t中的字符都出现过的时候,就记录当前子串的长度
- ️️ 还有一点就是,恢复滑动窗口左窗口左侧的所有值,这样当再出现的时候,不会误判
- 代码
class Solution {
public String minWindow(String s, String t) {
if(s == null || t == null || s.length() < t.length())
return "";
char[] s_ch = s.toCharArray();
char[] t_ch = t.toCharArray();
int[] map = new int[256];
//t字母对应的下标
for(int i = 0; i < t.length(); i ++){
map[t_ch[i]] ++;
}
int toMatch = t.length();
int minValue = Integer.MAX_VALUE;
int left = 0, right = 0;
int res = 0;
for(int i = 0; i < s.length(); i ++){
map[s_ch[i]] --;
if(map[s_ch[i]] >= 0)
toMatch --;
if(toMatch == 0){
// 当前已经找到了t中的所有字符
// 这里为什么这么写,是因为如果前面出现了重复的t的字符,但是又不属于窗口内的话,不加回来,就会导致窗口内的字符也会被丢弃,因为小雨0了
while(map[s_ch[left]] < 0){
map[s_ch[left ++]] ++;
}
if(minValue > (right - left)){
minValue = right - left + 1;
res = left;
}
toMatch ++;
map[s_ch[left ++]] ++;
}
right ++;
}
return minValue == Integer.MAX_VALUE ? "" : s.substring(res, res + minValue);
}
}边栏推荐
- Callable实现多线程
- 【FPGA】名词缩写
- 蹭个热度-请勿打开
- How to delete statements audit log?
- Audio codec, using FAAC to implement AAC encoding
- 云平台下ESB产品开发步骤说明
- CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
- A simple JVM tuning, learn to write it on your resume
- "98 BST and Its Verification" of the 13th day of leetcode brushing series of binary tree series
- 【FPGA】SDRAM
猜你喜欢

QueryDet:级联稀疏query加速高分辨率下的小目标检测

es-head plugin insert query and conditional query (5)

STC8H development (15): GPIO drive Ci24R1 wireless module

LeetCode刷题第11天字符串系列之《 58最后一个单词长度》
![[FPGA] Design Ideas - I2C Protocol](/img/ad/7bd52222e81b81a02b72cd3fbc5e16.png)
[FPGA] Design Ideas - I2C Protocol

I didn't expect MySQL to ask these...

STC8H开发(十五): GPIO驱动Ci24R1无线模块

Get Qt installation information: including installation directory and various macro addresses

Homework 8.10 TFTP protocol download function

使用jackson解析json数据详讲
随机推荐
Get Qt installation information: including installation directory and various macro addresses
What kind of programming trading strategy types can be divided into?
机器学习可以应用在哪些场景?机器学习有什么用?
How can users overcome emotional issues in programmatic trading?
Unity2D animation (1) introduction to Unity scheme - animation system composition and the function of use
En-us is an invalid culture error solution when Docker links sqlserver
MySQL数据库存储引擎以及数据库的创建、修改与删除
【Yugong Series】August 2022 Go Teaching Course 036-Type Assertion
Enter the starting position, the ending position intercepts the linked list
高度塌陷问题的解决办法
App Basic Framework Construction丨Log Management - KLog
【愚公系列】2022年08月 Go教学课程 035-接口和继承和转换与空接口
Getting Started with Raspberry Pi (5) System Backup
QueryDet:级联稀疏query加速高分辨率下的小目标检测
Qnet弱网测试工具操作指南
CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
rub the heat - do not open
Callable实现多线程
What is machine learning?Explain machine learning concepts in detail
Watch to monitor