leetcode73:minmum-window-substring
题目描述
T ="ABC"
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.
输出
"BANC"
链接:https://www.nowcoder.com/questionTerminal/c466d480d20c4c7c9d322d12ca7955ac?f=discussion
来源:牛客网
class Solution {public: //维持一个窗口滑动,左边是left,右边是right,然后判断是否包含T string minWindow(string S, string T) { string result; if(!S.size() || !T.size()) { return result; } map<char, int>Tmap; //存储T字符串里字符,便于与S匹配 int left = 0; //左边窗口,起始为0 int count = 0; //计数器,对窗口内T串字符数量进行计数,起始为0 //装载T串 int minlen = S.size() + 1; //最小窗口,便于比较最后取最小的,初始化取最大 for(int i = 0; i < T.size(); ++i) { Tmap[T[i]]++; } //移动右边窗口 for(int right = 0; right < S.size(); ++right) { if(Tmap.find(S[right]) != Tmap.end()) //当窗口内部有T中的字符 { if(Tmap[S[right]] > 0) { count++; //计数器+1 } Tmap[S[right]]--; //去掉包含的,剩下都是没包含的 while(count == T.size())//当窗口内有T的所有字符,就要开始移动左窗口啦 { if(Tmap.find(S[left]) != Tmap.end()) { //好了,这就是一个包含字符串的窗口范围:left ~ right, //判断是否比当前窗口还小,是就取串 if(minlen > right - left + 1) { //更新窗口大小 minlen = right -left + 1; result = S.substr(left, right - left + 1); } //舍弃窗口左边字符串,继续移动窗口 Tmap[S[left]]++; if(Tmap[S[left]] > 0) //如果左边连续相同,则count不递减,窗口需要继续向右移动 { count--; } } left++; //移动左窗口 } } } return result; }};leetcode73:minmum-window-substring的更多相关文章
- 53. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...
- leetcode76. Minimum Window Substring
leetcode76. Minimum Window Substring 题意: 给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符. 例如, S ="AD ...
- 【LeetCode】76. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- [LeetCode] Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [Leetcode][JAVA] Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- Java for LeetCode 076 Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- lintcode 中等题:minimum window substring 最小子串覆盖
题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...
随机推荐
- Angluar2 项目搭建
一 使用 Angular CLI 官方脚手架 1.安装 cli npm install -g @angular/cli 2.创建工作空间和初始应用 ng new my-app 二 tsLint 代码格 ...
- lua 1.0 源码分析 -- 1 lua 的虚拟指令
lua的解释器拿到 lua 编写的源码,首先进行解析,就是进行词法分析和语法分析,将源码转换成 lua 的指令集,然后执行这个指令集. lua 源码: function f(val) return v ...
- 多测师讲解python_模块(导入模块和内置模块)_高级讲师肖sir
#自定义模块# from aaa import * #指定导入某个包中具体的类.函数.方法## A.fun1(2,2) #import +模块名 :# # import +模块名+.+.+# # 导入 ...
- 多测师讲解python __for 循环___高级讲师肖sir
横向输出 1.遍历字符串 2.遍历列表 3.遍历元组 方法一: 方法二: 方法三: #循环字典:方法一# dict1={"name":"zhihao",&quo ...
- es6深层次数组深拷贝
let arr = [ { label: '1', children: [1, 2] } ] let a = [{...arr[0]}] ...
- c++程序设计实践——银行系统
银行系统 本科大二程序设计实践的作业,算是一个比较简单的项目吧,主要使用的编程范式有面向对象编程 其中引入<multimap><map>头文件实现多映射输出存取记录 引入< ...
- centos8平台redis5日志按天分割
一,创建日志的备份目录 [root@yjweb crontab]# mkdir /data/logs/redislogsbackup 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https:// ...
- echo 输出颜色
shell脚本里使用echo输出颜色 echo命令颜色显示: echo: -n: 不换行. -e:让转移符生效. \t(tab) \n (换行) 实例: $ echo ...
- centos8平台使用nethogs基于进程监控网络流量
一,nethogs的作用: 按进程或程序实时统计网络带宽使用率 我们查看流量的占用时,知道来源的ip.访问的端口,还不足以帮我们确认到进程, 而nethogs则可以让我们查看每个进程所占用的流量带宽 ...
- switch host 切换本地host
百度网盘提取地址 提取码: 753r 下载后放到软件目录即可使用