Minimum Window Substring
来源:互联网 发布:淘宝武士刀 编辑:程序博客网 时间:2024/05/16 15:00
尾指针不断往后扫,当扫到有一个窗口包含了所有T的字符,头指针收缩头指针。得到窗口最小的情况
class Solution {public: string minWindow(string S, string T) { int slen = S.size(); int tlen = T.size(); // record T[i] int need[256] = {0},has[256] = {0}, cnt = 0, ans = slen+1, mBegin, mEnd ; for(int i = 0; i < tlen; i++) ++need[T[i]]; for(int begin = 0, end = 0; end < slen; end++){ if(need[S[end]] == 0) continue; //find T[i] ++has[S[end]]; if(has[S[end]] <= need[S[end]]) ++cnt; //number of map if(cnt == tlen){ while(need[S[begin]] == 0 || has[S[begin]] > need[S[begin]]){ if(has[S[begin]] > need[S[begin]]) --has[S[begin]]; begin++; } int l = end - begin+1; if(l < ans){ mBegin = begin; mEnd = end; ans = l; } } } return ans <= slen?S.substr(mBegin, mEnd- mBegin+1):""; }};
0 0
- LeetCode: Minimum Window Substring
- LeetCode Minimum Window Substring
- LeetCode : Minimum Window Substring
- [LeetCode] Minimum Window Substring
- [Leetcode] Minimum Window Substring
- Minimum Window Substring
- LeetCode: Minimum Window Substring
- Minimum Window Substring
- [LeetCode]Minimum Window Substring
- Minimum Window Substring
- Minimum Window Substring
- [leetcode]Minimum Window Substring
- Minimum Window Substring
- Leetcode:Minimum Window Substring
- LeetCode-Minimum Window Substring
- [leetcode] Minimum Window Substring
- [leetcode] Minimum Window Substring
- leetcode Minimum Window Substring
- Cocos2d-x 3.2 大富翁游戏项目开发-第十八部分 问号随机事件
- addHeaderView
- Android中用StrictMode检测内存泄露
- 原创文章
- 如何在centos安装python-mysql?
- Minimum Window Substring
- mysql数据库存放目录修改
- 实验二、 线性表实验
- C#的委托
- IIS无SVC文件托管WCF服务
- C# 关联键盘按钮 (KeyChar/KeyCode值 KeyPress/KeyDown事件 区别)
- 实验三:栈和队列实验
- Java逻辑与位操作
- datagridview单元格数据验证