当前位置:网站首页>Leetcode76. 最小覆盖子串
Leetcode76. 最小覆盖子串
2022-08-03 15:49:00 【Java全栈研发大联盟】
题目传送地址: https://leetcode.cn/problems/minimum-window-substring/
运行效率
代码如下:
class Solution {
//滑动窗口解法 为了提升两个字符串的对比效率, 于是用了map,动态维护滑动窗口map
public static String minWindow(String s, String t) {
//处理边界情况
if ("".equals(s)) {
return "";
}
if (s.length() == 1) {
if (s.equals(t)) {
return s;
}
return "";
}
String result = "";
//维护一个滑动窗口的动态Map,用于标识窗口内各个字符出现的次数
Map<Character, Integer> tMap = getTmap(t);
Map<Character, Integer> sMap = new HashMap<>();
//滑动窗口
int left = 0; //滑动窗口的左指针
int right = 0; //滑动窗口的右指针
sMap.put(s.charAt(0), 1);
while (right < s.length()) {
if (right - left + 1 < t.length()) {
right++;
if (right < s.length()) {
putValToMap(s.charAt(right), sMap);
}
continue;
}
Character character = vaildStr(sMap, tMap);
if (character == null) {
//如果验证通过
String substring = s.substring(left, right + 1);
if ("".equals(result)) {
result = substring;
} else {
if (substring.length() < result.length()) {
result = substring;
}
}
deleteValToMap(s.charAt(left), sMap); //先从滑动窗口map里删除,然后再把左指针右移,顺序别搞反
left++;
} else {
right++;
if (right < s.length()) {
putValToMap(s.charAt(right), sMap);
}
while (right < s.length()) {
char c = s.charAt(right);
if (c == character) {
break;
}
right++;
if (right < s.length()) {
putValToMap(s.charAt(right), sMap);
}
}
}
if (result.length() == t.length()) {
return result;
}
}
return result;
}
public static void putValToMap(Character c, Map<Character, Integer> map) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
public static void deleteValToMap(Character c, Map<Character, Integer> map) {
if (map.containsKey(c)) {
Integer count = map.get(c);
if (count == 1) {
map.remove(c);
} else {
map.put(c, count - 1);
}
}
}
public static Map<Character, Integer> getTmap(String t) {
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < t.length(); i++) {
char c = t.charAt(i);
if (map.containsKey(c)) {
Integer count = map.get(c);
map.put(c, count + 1);
} else {
map.put(c, 1);
}
}
return map;
}
/** * 验证滑动窗口内的元素是否包含目标对象的所有元素 * @param sMap * @param tMap * @return 返回缺失的元素 */
public static Character vaildStr(Map<Character, Integer> sMap, Map<Character, Integer> tMap) {
Iterator<Character> iterator = tMap.keySet().iterator();
while (iterator.hasNext()) {
Character c = iterator.next();
Integer tCount = tMap.get(c);
if (sMap.containsKey(c)) {
Integer sCount = sMap.get(c);
if (sCount < tCount) {
return c;
}
} else {
return c;
}
}
return null;
}
}
边栏推荐
- Three key expectations for the crypto market in August Price moves north?Still expected to be in turmoil
- 2021年12月电子学会图形化一级编程题解析含答案:下雨
- 爬虫注意
- With a single operation, I improved the SQL execution efficiency by 10,000,000 times!
- Reptile attention
- 新一代网状网协议T-Mesh无线通信技术优势介绍
- Basic knowledge points in js - events
- 30W 2C(JD6606S + FP6652X2)BOM
- 2021年12月电子学会图形化四级编程题解析含答案:森林运动会
- A new round of competition for speech recognition has started. Will natural dialogue be the next commanding height?
猜你喜欢
随机推荐
聊聊这个SaaS领域爆火的话题
nodeJs--跨域
Fortinet产品导入AWS AMI操作文档
Daily practice------There are 10 numbers that are required to be output from large to small by selection method
【码蹄集新手村600题】将一个函数定义宏
为教育插上数字化的翅膀,网易云信发布「互联网+教育」整体解决方案
Ruoyi Ruoyi framework @DataScope annotation use and some problems encountered
ECCV 2022 | Relational Query-Based Temporal Action Detection Methods
如何用二分法搜索、查找旋转数组中是否含有某个(目标)值? leetcode 81.搜索旋转排序数组
深度学习GPU最全对比,到底谁才是性价比之王?
出海季,互联网出海锦囊之本地化
Ark server open tool, server tutorial win
2021年12月电子学会图形化四级编程题解析含答案:森林运动会
opencv 读取和写入路径有汉字的处理方法
CS免杀姿势
【899. Ordered Queue】
请问下,flink cdc监控oracle,我看源码是通过sid方式的,请问怎么改成service
0 code 4 steps to experience IoT devices on the cloud
Small Tools(4) 整合Seata1.5.2分布式事务
JS handwritten call apply bind (detailed) (interview)