当前位置:网站首页>每日一题-滑动窗口
每日一题-滑动窗口
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);
}
}
边栏推荐
- shell monitors gpu usage
- Echart地图的省级,以及所有地市级下载与使用
- Is there any way for kingbaseES to not read the system view under sys_catalog by default?
- 一文读懂 高性能可预期数据中心网络
- The last update time of the tables queried by the two nodes of the rac standby database is inconsistent
- Qnet弱网测试工具操作指南
- Which one to choose for mobile map development?
- What is third-party payment?
- 2022-08-10 The sixth group Hiding spring study notes
- The solution to the height collapse problem
猜你喜欢
Design and Realization of Employment Management System in Colleges and Universities
Differences and connections between distributed and clustered
多串口RS485工业网关BL110
移动端地图开发选择哪家?
Where can machine learning be applied?What is machine learning useful for?
Graphical LeetCode - 640. Solving Equations (Difficulty: Moderate)
使用jackson解析json数据详讲
The last update time of the tables queried by the two nodes of the rac standby database is inconsistent
E-commerce project - mall time-limited seckill function system
【FPGA】abbreviation
随机推荐
The development of the massage chair control panel makes the massage chair simple and intelligent
typedef defines the structure array type
什么是机器强化学习?原理是什么?
【FPGA】day18-ds18b20实现温度采集
这些云自动化测试工具值得拥有
你不知道的 console.log 替代品
What is machine learning?Explain machine learning concepts in detail
.NET service registration
Differences and connections between distributed and clustered
AI + medical: for medical image recognition using neural network analysis
MYSQLg advanced ------ clustered and non-clustered indexes
云平台下ESB产品开发步骤说明
机器学习中什么是集成学习?
[yu gong series] Go program 035-08 2022 interfaces and inheritance and transformation and empty interface
【FPGA】day21- moving average filter
程序化交易改变了什么?
阿里云发布3大高性能计算解决方案
leetcode刷题第13天二叉树系列之《98 BST及其验证》
Leetcode 108. 将有序数组转换为二叉搜索树
What problems should we pay attention to when building a programmatic trading system?