博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Minimum Window Substring
阅读量:7263 次
发布时间:2019-06-29

本文共 3916 字,大约阅读时间需要 13 分钟。

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 emtpy string "".

If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

 

Hide Tags
    
 

思路:

双指针,动态维护一个区间。尾指针不断往后扫,当扫到有一个窗口包含了所有 T 的字符后,

然后再收缩头指针,直到不能再收缩为止。最后记录所有可能的情况中窗口最小的

时间复杂度 O(n),空间复杂度 O(1)

class Solution {    public:        string minWindow(string S, string T)        {            if(S.empty() || T.empty() || S.size() < T.size())                return string();            vector
expect(256, 0); vector
appear(256, 0); for(int i = 0; i < T.size(); i++) { expect[T[i]] ++; } int minWidth = INT_MAX, min_start = 0; // int win_start = 0; int appearCharCnt = 0; for(int win_end = 0; win_end < S.size(); win_end++) { if(expect[S[win_end]] > 0) // this char is part of T { appear[S[win_end]]++; if(appear[S[win_end]] <= expect[S[win_end]]) appearCharCnt ++; } //cout << "appearCharCnt\t" <
<< endl; if(appearCharCnt == T.size()) { // shrink the start while (appear[S[win_start]] > expect[S[win_start]] || expect[S[win_start]] == 0) { appear[S[win_start]]--; win_start++; } if ((win_end - win_start + 1) < minWidth) { minWidth = win_end - win_start + 1; min_start = win_start; //cout << "min_start\t" <
<< endl; //cout << "min_width\t" <
<< endl; } } } if (minWidth == INT_MAX) return ""; else return S.substr(min_start, minWidth); }};

 

精简一下条件判断

class Solution {    public:        string minWindow(string S, string T)        {            if(S.empty() || T.empty() || S.size() < T.size())                return string();            vector
expect(256, 0); vector
appear(256, 0); for(int i = 0; i < T.size(); i++) { expect[T[i]] ++; } int minWidth = INT_MAX, min_start = 0; int win_start = 0; int appearCharCnt = 0; for(int win_end = 0; win_end < S.size(); win_end++) { appear[S[win_end]]++; if(appear[S[win_end]] <= expect[S[win_end]]) appearCharCnt ++; //cout << "appearCharCnt\t" <
<< endl; if(appearCharCnt == T.size()) { // shrink the win start while (appear[S[win_start]] > expect[S[win_start]] ) { appear[S[win_start]]--; win_start++; } if ((win_end - win_start + 1) < minWidth) { minWidth = win_end - win_start + 1; min_start = win_start; //cout << "min_start\t" <
<< endl; //cout << "min_width\t" <
<< endl; } } } if (minWidth == INT_MAX) return ""; else return S.substr(min_start, minWidth); }};

 

转载地址:http://xsldm.baihongyu.com/

你可能感兴趣的文章
EntityFramework之你不知道的那些事(七)
查看>>
YY博客园UML用例图-活动图-状态图之博客模块
查看>>
iOS - UITextView
查看>>
PAT Basic 1073. 多选题常见计分法
查看>>
SQL SERVER 常用知识整理
查看>>
基于局部敏感哈希的协同过滤推荐算法之E^2LSH
查看>>
What is the difference between page and block in operating system?
查看>>
配置rman备份的默认备份路径_default path
查看>>
MM 调查卷(转)
查看>>
深入理解 Promise 五部曲:2. 控制权转换问题
查看>>
dir listing 目录文件列表索引
查看>>
MySQL中的undo截断(r11笔记第89天)
查看>>
《Programming WPF》翻译 第6章 3.二进制资源
查看>>
c#的DateTime.Now函数详解
查看>>
Spring Boot + Websocket + Thymeleaf + Lombok
查看>>
linux命令之uniq
查看>>
SugarCRM 插件介绍
查看>>
aliyun.com
查看>>
cygwin的使用(2)
查看>>
App域名劫持之DNS高可用 - 开源版HttpDNS方案详解(转)
查看>>