LeetCode-Minimum Window Substring
2013-08-10 11:31
507 查看
class Solution { public: string minWindow(string S, string T) { // Start typing your C/C++ solution below // DO NOT write int main() function //两个指针,当右指针扫描至包含所有T中字母时,收缩左指针 vector<int> flag(128, 0); int tLen = 0; for (size_t i = 0; i < T.length(); ++i) { ++flag[T[i]]; ++tLen; } vector<int> curFlag(128, 0); int curBeg = -1; int curLen = 0; int beg = -1; int end = -2; for (size_t i = 0; i < S.length(); ++i) { if (flag[S[i]] > 0) { ++curFlag[S[i]]; if (flag[S[i]] >= curFlag[S[i]]) { ++curLen; } if (curBeg < 0) { curBeg = i; } if (curLen == tLen) { for (;curBeg < i; ++curBeg) { char index = S[curBeg]; if (flag[index] > 0) { if (curFlag[index] > flag[index]) { --curFlag[index]; } else { break; } } } if (i - curBeg < end - beg) { beg = curBeg; end = i; } } } } if (beg == -1) { return ""; } else { return S.substr(beg, end - beg + 1); } } };
相关文章推荐
- Leetcode Minimum Window Substring
- LeetCode Minimum Window Substring
- leetcode 76 Minimum Window Substring/ 209 Minimum Size Subarray Sum
- LeetCode-Minimum Window Substring-最小窗口子串-滑动窗口算法(尺取法)
- LeetCode : Minimum Window Substring
- [leetcode-76]Minimum Window Substring(java)
- Leetcode -- Minimum Window Substring
- LeetCode - Minimum Window Substring
- LeetCode(76) Minimum Window Substring
- [leetcode刷题系列]Minimum Window Substring
- [leetcode] Minimum Window Substring
- Minimum Window Substring @LeetCode
- leetcode minimum window subString
- LeetCode之Minimum Window Substring
- LeetCode Minimum Window Substring
- LeetCode: Minimum Window Substring
- LeetCode Online Judge 题目C# 练习 - Minimum Window Substring
- 【leetcode】Minimum Window Substring
- Leetcode -- Minimum Window Substring
- LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]