当前位置:网站首页>727. 最小窗口子序列 滑动窗口
727. 最小窗口子序列 滑动窗口
2022-07-23 00:47:00 【钰娘娘】
727. 最小窗口子序列
给定字符串
SandT,找出S中最短的(连续)子串W,使得T是W的 子序列 。如果
S中没有窗口可以包含T中的所有字符,返回空字符串""。如果有不止一个最短长度的窗口,返回开始位置最靠左的那个。示例 1:
输入: S = "abcdebdde", T = "bde" 输出:"bcde" 解释: "bcde" 是答案,因为它在相同长度的字符串 "bdde" 出现之前。 "deb" 不是一个更短的答案,因为在窗口中必须按顺序出现 T 中的元素。注:
- 所有输入的字符串都只包含小写字母。All the strings in the input will only contain lowercase letters.
S长度的范围为[1, 20000]。T长度的范围为[1, 100]。
做题结果
成功,勉强用滑窗写出来了
方法:滑动窗口
1. 确定起点,和第一个字符不匹配的一律跳过
2. 进行完全匹配,如果能完成匹配,则添加到答案
class Solution {
public String minWindow(String s1, String s2) {
int m = s1.length();
int n = s2.length();
int ans = 0;
int start = 0;
for(int i =0 ; i < m-n+1; i++){
while (i<m&&s1.charAt(i)!=s2.charAt(0)) ++i;
int j = i;
int k = 0;
for(; j < m&&k<n; j++){
if (s1.charAt(j)==s2.charAt(k)){
++k;
}
}
if(k==n&&(j-i<ans||ans==0)){
ans = j-i;
start = i;
}
}
return s1.substring(start,start+ans);
}
}边栏推荐
- SQL Server database design -- select statement 2
- 解读机器人视觉类别及应用原理
- Is it safe to open an account online? How about Galaxy Securities
- disruptor框架无锁实现生产者消费者代码实例
- 基于SSM的博客系统【带后台管理】
- 数学建模——插值拟合
- Flutter 3.0
- IDM downloader free high-quality win download tool without restrictions
- 吉利星瑞:从产品技术赋能到文化自信
- [advanced mathematics] elementary transformation of matrix and determinant
猜你喜欢

ADB 命令结合 monkey 的简单使用,超详细

UGUI源码解析——IMaterialModifier

What are the seven layers of OSI's seven layer model? What is the role of each layer? This article is clear!

College students downloaded 2578 documents abnormally, and the IP of the University of Social Sciences of China was banned by a database

UGUI源码解析——MaskUtilities

【云原生】风云暗涌的时代,DBA们的利刃出鞘了

SQL Server database design -- select statement 2

【并发编程】第二章:从核心源码深入ReentrantLock锁

SQL Server 数据库设计--SELECT语句之二
![[concurrent programming] Chapter 2: go deep into the reentrantlock lock lock from the core source code](/img/df/f29eed667c2a7dc02d93ac3f424198.png)
[concurrent programming] Chapter 2: go deep into the reentrantlock lock lock from the core source code
随机推荐
Construction of mGRE network
-bash: wget: 未找到命令
UGUI源码解析——StencilMaterial
How many of the 50 classic computer network interview questions can you answer? (III)
Internet download manager is simply a killer of downloaders
SQL用户表的通用设计
【云原生】风云暗涌的时代,DBA们的利刃出鞘了
Arduino框架下合宙ESP32C3 +1.8“TFT液晶屏通过TFT_eSPI库驱动显示
解析steam与创客教育课堂的统筹规划
Internet Download Manager简直就是下载器中的大杀器
[zero based BLDC series] brushless DC motor control principle based on Hall sensor
College students downloaded 2578 documents abnormally, and the IP of the University of Social Sciences of China was banned by a database
OSPF的路由控制
差分数组操作的一些性质
Wireshark packet capture tool basic use
基于共轭梯度法的对抗攻击
跨境电商旺季来临,汇付国际收款0费率助你赢战旺季!
Flutter linear layout, filling
PMP备考心得 | 好的习惯、好的过程、好的结果
驱动单片机硬件调试器的一些开源库总结(包含stlink调试器)