LeetCode - Minimum Window Substring 题解
来源:互联网 发布:斯普特尼克恋人 知乎 编辑:程序博客网 时间:2024/06/10 03:20
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC"
.
Note:
If there is no such window in S that covers all characters in T, return the emtpy string ""
.
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
分析:
一前一后两个指针,后指针向后寻找到包含T的区间,移动前指针到最优位置,判断一次,如此循环。
class Solution {public: string minWindow(string S, string T) { int Need[256], Now[256]; for(int i = 0;i < 256; i++){ Need[i] = Now[i] = 0; } int LS = S.length(); int LT = T.length(); for(auto ch:T) Need[ch] ++; int i = 0, j = 0, foundNum = 0; int minLength = LS + 1, Begin = 0, End = 0; while(j < LS){ if(!Need[S[j]]){ j++; continue; } Now[S[j]]++; if(Now[S[j]] <= Need[S[j]]) ++foundNum; if(foundNum == LT){ while(Need[S[i]] == 0 || Now[S[i]] > Need[S[i]]){ if(Now[S[i]] > Need[S[i]]) --Now[S[i]]; i++; } int l = j - i + 1; if(l < minLength){ minLength = l; Begin = i; End = j; } } j++; } return minLength <= LS ? S.substr(Begin, minLength) :""; }};
0 0
- LeetCode题解:Minimum Window Substring
- LeetCode - Minimum Window Substring 题解
- LeetCode 题解(101): Minimum Window Substring
- LeetCode题解——Minimum Window Substring
- leetcode:Minimum Window Substring题解(Java)
- leetcode题解-76. Minimum Window Substring
- LeetCode题解–76. Minimum Window Substring
- LeetCode Algorithms 76. Minimum Window Substring 题解
- leetcode题解c++ | 76. Minimum Window Substring
- LeetCode No.76 Minimum Window Substring 题解
- LeetCode: Minimum Window Substring
- LeetCode Minimum Window Substring
- LeetCode : Minimum Window Substring
- [LeetCode] Minimum Window Substring
- [Leetcode] Minimum Window Substring
- LeetCode: Minimum Window Substring
- [LeetCode]Minimum Window Substring
- [leetcode]Minimum Window Substring
- 牛人网站和博客---站得高看得更远(CV类)
- PhotoShop算法实现--色调/饱和度调整(四)
- python 回调函数和回调方法的实现
- 意图——学习笔记总结
- 移动端 video 的autoplay不起作用的解决方案
- LeetCode - Minimum Window Substring 题解
- setinel分布式部署+redis主从集群+java客户端连接资料整理分享
- Java代码优化札记
- LeetCode Search in Rotated Sorted Array
- 面向对象的六大原则
- Bzoj-1096
- IOS:iscroll里的元素对click/tap事件不响应
- 医疗时鲜资讯:移动医疗 or 互联网医疗 or 远程医疗?
- AspNetPager 控件实现真分页功能