leetcode 67.Minimum Window Substring

来源:互联网 发布:电视机的网络接口 编辑:程序博客网 时间:2024/06/08 08:00
// 双指针, 哈希表class Solution {public:string minWindow(string s, string t) {vector<int> cnt(128, 0);for (int e : t) { cnt[e]++; }int begin, minWindowLen = INT_MAX;int tLen = t.size();for (int tail = 0, head = 0; head < s.size();) {// [tail, head)//统计t中还剩多少没有字符被覆盖掉    if (cnt[s[head++]]-- > 0) { tLen--; }// t被s中[tail... head)(开区间)位置的子串覆盖if (tLen == 0) {// 更新tail, 将从tail开始的一段字串剔除,因为重复覆盖了while (cnt[s[tail]] < 0) { cnt[s[tail++]]++; } if (minWindowLen > head - tail) {minWindowLen = head - tail;begin = tail;}// 移动tail,使得覆盖失效cnt[s[tail++]]++; tLen++;  }}return minWindowLen == INT_MAX ? "" : s.substr(begin, minWindowLen);}};

0 0
原创粉丝点击