076 Minimum Window Substring 最小窗口子字符串
给定一个字符串 S 和一个字符串 T,找到 S 中的最小窗口,它将包含复杂度为 O(n) 的 T 中的所有字符。
示例:
S = "ADOBECODEBANC"
T = "ABC"
最小窗口是 "BANC".
注意事项:
如果 S 中没有覆盖 T 中所有字符的窗口,则返回空字符串 ""。
如果有多个这样的窗口,你将会被保证在 S 中总是只有一个唯一的最小窗口。
详见:https://leetcode.com/problems/minimum-window-substring/description/
Java实现:
class Solution {
public String minWindow(String s, String t) {
if (s == null || t == null || s.length() < t.length()){
return "";
}
// HashMap的key为t中各个字符,value为对应字符个数
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (char c : t.toCharArray()) {
if (!map.containsKey(c)){
map.put(c, 0);
}
map.put(c, map.get(c) + 1);
}
// minLeft为最小窗口左下标,minLen为最小长度,count用来计数
int minLeft = 0, minLen = s.length() + 1, count = 0;
int left = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (map.containsKey(c)) {
// 如果map.get(c)说明t中还有字符没有包含,计数器+1
if (map.get(c) > 0){
count++;
}
map.put(c, map.get(c) - 1);
}
// 如果left到i中包含t中所有字符
while (count == t.length()) {
if (i - left + 1 < minLen) {
minLeft = left;
minLen = i - left + 1;
}
c = s.charAt(left);
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
if (map.get(c) > 0){
count--;
}
}
left++;
}
}
if (minLen > s.length()){
return "";
}
return s.substring(minLeft, minLeft + minLen);
}
}
参考:https://www.nowcoder.com/questionTerminal/c466d480d20c4c7c9d322d12ca7955ac
详见:https://www.cnblogs.com/grandyang/p/4340948.html
076 Minimum Window Substring 最小窗口子字符串的更多相关文章
- Java for LeetCode 076 Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- leetcode76. Minimum Window Substring
leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- [Swift]LeetCode76. 最小覆盖子串 | Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 【LeetCode】76. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- 53. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- LeetCode(76) Minimum Window Substring
题目 Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...
随机推荐
- Apache禁止或允许固定IP访问特定目录、文件、URL
1. 禁止访问某些文件/目录 增加Files选项来控制,比如要不允许访问 .inc 扩展名的文件,保护php类库: <Files ~ "\.inc$"> Order a ...
- workerman介绍
WorkerMan的特性 1.纯PHP开发 使用WorkerMan开发的应用程序不依赖php-fpm.apache.nginx这些容器就可以独立运行. 这使得PHP开发者开发.部署.调试应用程序非常方 ...
- BZOJ1251 序列终结者(Splay平衡树)(占位)
网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量…… ...
- ScikitLearn 学习器类型
源自在线课程的学习:http://www.studyai.com/course/detail/d086826e9be84b818f9c54893633663d
- 51nod 1443 路径和树——最短路生成树
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1443 不只是做一遍最短路.还要在可以选的边里选最短的才行. 以为是 ...
- MVC之Control中使用AOP
原文转载自http://www.cnblogs.com/iamlilinfeng/archive/2013/03/02/2940162.html 本文目标 一.能够使用Control中的AOP实现非业 ...
- array / matrix subarray/submatrix sum
Maximal Subarray Sum : O(n) scan-and-update dynamic programming, https://en.wikipedia.org/wiki/Maxim ...
- Adding In-App Purchase to your iOS and OS X Applications
Adding In-App Purchase to your iOS and OS X Applications In-App Purchase allows you to sell addition ...
- linux命令配置IP详解
在Linux系统中,TCP/IP网络是通过若干个文本文件进行配置的,有时需要编辑这些文件来完成联网工作. vi /etc/sysconfig/network-scripts/ifcfg-eth0 :进 ...
- TypeScript完全解读(26课时)_汇总贴
ECMAScript 6 入门:http://es6.ruanyifeng.com/ 官网:http://www.typescriptlang.org/ 中文网:https://www.tslang. ...