当前位置:网站首页>每日一题-滑动窗口
每日一题-滑动窗口
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);
}
}边栏推荐
- Interchangeability and Measurement Techniques - Tolerance Principles and Selection Methods
- What is third-party payment?
- leetCode刷题14天二叉树系列之《 110 平衡二叉树判断》
- DNS separation resolution and intelligent resolution
- 程序化交易改变了什么?
- App Basic Framework Construction丨Log Management - KLog
- Watch to monitor
- CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
- Is Redis old?Performance comparison between Redis and Dragonfly
- rub the heat - do not open
猜你喜欢

Description of ESB product development steps under cloud platform

【FPGA】day22-SPI protocol loopback

【FPGA】day20-I2C读写EEPROM

Use jackson to parse json data in detail

【FPGA】day18-ds18b20实现温度采集

机器学习怎么学?机器学习流程

Multi-serial port RS485 industrial gateway BL110

Kubernetes集群搭建Zabbix监控平台

Power Cabinet Data Monitoring RTU

Interchangeability Measurements and Techniques - Calculation of Deviations and Tolerances, Drawing of Tolerance Charts, Selection of Fits and Tolerance Classes
随机推荐
程序化交易的策略类型可以分为哪几种?
Binary tree related code questions [more complete] C language
Will oracle cardinality affect query speed?
Echart地图的省级,以及所有地市级下载与使用
机器学习是什么?详解机器学习概念
es-head插件插入查询以及条件查询(五)
.NET 服务注册
How can users overcome emotional issues in programmatic trading?
【FPGA】day18-ds18b20实现温度采集
LeetCode814算题第15天二叉树系列值《814 二叉树剪枝》
QueryDet: Cascading Sparse Query Accelerates Small Object Detection at High Resolution
rub the heat - do not open
Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array
Interchangeability and Measurement Techniques - Tolerance Principles and Selection Methods
[FPGA] Design Ideas - I2C Protocol
EasyCVR接入海康大华设备选择其它集群服务器时,通道ServerID错误该如何解决?
构建程序化交易系统需要注意什么问题?
MySQL数据库存储引擎以及数据库的创建、修改与删除
拼多多店铺营业执照相关问题
En-us is an invalid culture error solution when Docker links sqlserver