LeetCode: Minimum Window Substring
来源:互联网 发布:蓝鸥unity3d培训多少钱 编辑:程序博客网 时间:2024/05/01 10:09
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.
字符哈希+双指针。
class Solution {public: string minWindow(string S, string T) { // Start typing your C/C++ solution below // DO NOT write int main() function int nT = T.size(); int nS = S.size(); int needToFind[256] = {0}; for (int i = 0; i < nT; ++i) ++needToFind[T[i]]; int hasFound[256] = {0}; int minBegin; int minEnd; int minWindow = nS + 1; int count = 0; char ch; for (int begin = 0, end = 0; end < nS; ++end) { if (needToFind[S[end]] == 0) continue; ch = S[end]; ++hasFound[ch]; if (hasFound[ch] <= needToFind[ch]) ++count; if (count == nT) { while (needToFind[S[begin]] == 0 || hasFound[S[begin]] > needToFind[S[begin]]) { if (hasFound[S[begin]] > needToFind[S[begin]]) --hasFound[S[begin]]; ++begin; } int length = end - begin + 1; if (length < minWindow) { minBegin = begin; minEnd = end; minWindow = length; } } } return minWindow <= nS ? S.substr(minBegin, minEnd-minBegin+1) : ""; }};
- 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
- 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
- LeetCode - Minimum Window Substring
- LeetCode:Minimum Window Substring
- GetWindowRect和GetClientRect的使用对比
- Fab CEO:创办四家公司教给我的90件事
- PHP和ActiveMQ集成
- 黑莓9900 只能用WIFI登录qq 不能用手机网络登录qq
- oracle查询所有表名
- LeetCode: Minimum Window Substring
- 修改网页编码出现的问题
- android之使用get和post方式向服务器提交请求
- hdu~2795~线段树
- MyEclipse连接数据库异常解决
- XML(1)
- JavaScript 浏览器检测方法。。。
- iOS越狱开发之MobileSubstrate介绍
- Java中十六进制转换 Integer.toHexString()