每日一练——Minimum Window Substring
来源:互联网 发布:乐清市知临中学 编辑:程序博客网 时间:2024/06/14 14:44
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.
Subscribe to see which companies asked this question
<span style="font-family:Microsoft YaHei;font-size:12px;">class Solution {public: string minWindow(string s, string t) { int start, end; start = end = 0; int count = 0; int mps[128] = {0}, mpt[128] = {0}; for (int i = 0; i < t.size(); i++) { mpt[t[i]] ++; } int m = t.size(); string min_str; while (start < s.size() && end < s.size()) { while (end < s.size() && count < m) { mps[s[end]]++; if (mps[s[end]] <= mpt[s[end]] )//mps[s[end]]增加1后未达到或刚好达到匹配数量,那么count++ { count++; } end++; } if (count < m) { return min_str; } while (start < s.size() && count == m) { mps[s[start]]--; if (mps[s[start]] < mpt[s[start]] )//mps[s[start]]减少1刚好少于匹配数量,那么count-- { count--; } start++; } string tmp = s.substr(start - 1, end - start + 1); if (min_str.empty() || min_str.size() > tmp.size()) { min_str = tmp; } } return min_str; }};</span>
0 0
- 每日一练——Minimum Window Substring
- 20171020—每日一练
- 20171019—每日一练
- LeetCode —— Minimum Window Substring
- leetcode 076 —— Minimum Window Substring
- LeetCode题解——Minimum Window Substring
- LeetCode76——Minimum Window Substring
- leetcode——Minimum Window Substring
- (每日算法)LeetCode---Minimum Window Substring (最小子串窗口)
- 每日一练——2016.2.24
- 每日一练——2016.2.25
- 每日一练——H-Index
- 每日一练——House Robber
- 每日一练——Single Number
- 每日一练——洗牌
- 黑白卡片——每日一练
- 15.5—细节实现题—Minimum Window Substring
- LeetCode: Minimum Window Substring
- html5+css 三列布局
- 理解变量的作用域
- LeetCode 067 Add Binary
- VC之fread函数和fwrite函数
- 我的梦想是架构师,我不要当程序猿!
- 每日一练——Minimum Window Substring
- Android 动画
- 实用代码块记录4
- 学徒浅析Android开发:杂谈——WebView的url跳转时方法执行顺序
- Android之简单拨号器的实现
- 查看自己linux系统的版本:
- Codeforces Round #360 (Div. 1)C - The Values You Can Make
- 算法探究之选择排序
- 用spark(spark-shell),从本地文件创建一个RDD