当前位置:网站首页>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;
}
}
边栏推荐
- How to use binary search and find whether the rotation in the array contains a (target) value?Rotate the sorted array leetcode 81. Search
- 2021年12月电子学会图形化二级编程题解析含答案:消灭蝙蝠
- 用户侧有什么办法可以自检hologres单表占用内存具体是元数据、计算、缓存的使用情况?
- 扩展欧几里得求逆元实例
- STM32的HAL和LL库区别和性能对比
- 0 code 4 steps to experience IoT devices on the cloud
- How to prevent hacking Windows server security Settings
- How Navicat connects to MySQL on a remote server
- 分享一款免费OPC UA服务器
- 49 万奖金等你来拿!第四届实时计算 Flink 挑战赛启动,Beyond Stream Processing!
猜你喜欢
Neural networks, cool?
如何选择合适的损失函数,请看......
With a single operation, I improved the SQL execution efficiency by 10,000,000 times!
Windows服务器如何防止黑客入侵的安全设置
AI+BI+可视化,Sugar BI架构深度剖析
开源一夏 | 阿里云物联网平台之极速体验
49 万奖金等你来拿!第四届实时计算 Flink 挑战赛启动,Beyond Stream Processing!
AI也有健忘症?英国41岁教授专访:解决灾难性遗忘
A new round of competition for speech recognition has started. Will natural dialogue be the next commanding height?
【Unity入门计划】制作RubyAdventure01-玩家的创建&移动
随机推荐
Essentially a database data recovery 】 【 database cannot read data recovery case
Basic knowledge points in js - events
Research on power flow in DC microgrid based on Newton's method (Matlab code implementation)
2021年12月电子学会图形化四级编程题解析含答案:森林运动会
leetcode: 899. Ordered Queue [Thinking Question]
[Code Hoof Set Novice Village 600 Questions] Define a function as a macro
破解数字化转型困局,企业分析协同场景案例解析
一通骚操作,我把SQL执行效率提高了10000000倍!
8月份加密市场的三个关键预期 价格虽向北移动?预计仍将处于动荡之中
DC-DC 2C (40W/30W) JD6606SX2 power back application
Fortinet产品导入AWS AMI操作文档
方舟开服工具、服务器教程win
Optimal Power Flow (OPF) for High Voltage Direct Current (HVDC) (Matlab code implementation)
Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行(含系列目录)。
方舟开服教程win
自定SvgIcon公用组件
技术干货|如何将 Pulsar 数据快速且无缝接入 Apache Doris
【码蹄集新手村600题】将一个函数定义宏
2021年12月电子学会图形化二级编程题解析含答案:消灭蝙蝠
全新探险者以40万的产品击穿豪华SUV价格壁垒