LeetCode(76) Minimum Window Substring
来源:互联网 发布:csol解封软件 编辑:程序博客网 时间:2024/05/29 11:15
题目
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 empty string “”.
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
分析
在字符串 S 中查找最小的窗口,使窗口中全部包含字符串T 中的字符,(不按顺序)。
需要注意的地方:
- T 中的字符可以重复,窗口需要重复包含。
- 测试 实验中的是SCII字符 ,所以可以用128位数组代替 hash table。
AC代码
class Solution {public: string minWindow(string S, string T) { if (S.empty() || T.empty()) { return ""; } int count = T.size(); int require[128] = { 0 }; bool chSet[128] = { false }; for (int i = 0; i < count; ++i) { require[T[i]]++; chSet[T[i]] = true; } int i = -1; int j = 0; int minLen = INT_MAX; int minIdx = 0; while (i < (int)S.size() && j < (int)S.size()) { if (count) { i++; require[S[i]]--; if (chSet[S[i]] && require[S[i]] >= 0) { count--; } }//if else { if (minLen > i - j + 1) { minLen = i - j + 1; minIdx = j; } require[S[j]]++; if (chSet[S[j]] && require[S[j]] > 0) { count++; } j++; }//else }//while if (minLen == INT_MAX) { return ""; } return S.substr(minIdx, minLen); }};
GitHub测试程序源码
0 0
- LeetCode(76) Minimum Window Substring
- LeetCode 76 Minimum Window Substring
- LeetCode 76 Minimum Window Substring
- [leetcode 76] Minimum Window Substring
- [leetcode] 76 Minimum Window Substring
- LeetCode(76)Minimum Window Substring
- LeetCode 76 Minimum Window Substring
- leetcode || 76、Minimum Window Substring
- leetcode-76 Minimum Window Substring
- leetcode 76: Minimum Window Substring
- leetcode 76: Minimum Window Substring
- Leetcode#76||Minimum Window Substring
- leetcode 76 Minimum Window Substring
- leetcode 76:Minimum Window Substring
- Leetcode-76 Minimum Window Substring
- Leetcode 76 Minimum Window Substring
- LeetCode 76 Minimum Window Substring
- LeetCode 76 minimum window substring
- Wins 的上帝模式
- cocos2dx 3.x下cocosstudio使用
- swift 学习记录(类和结构体)
- 怎么用脚本选中一个物体,然后显示它的Inpsector面板
- [多线程之一]-NSThread银行取款问题
- LeetCode(76) Minimum Window Substring
- GridView自定义分页
- java 面试问题
- Android通知Notification入门小例子(一)
- online_judge_1126
- Rethinking the Inception Architecture for Computer Vision 笔记
- 实惠快速:海淘买鞋的经历:6PM +笨鸟海淘转运(单号: CN150815052072)
- jQ事件及操作大汇总
- 接口回调