76. Minimum Window Substring (String, Map)
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.
思路:问题可以转变为,T中各个字符的数量<= window中的这些字符的数量,所以用map纪录字符与字符数量的关系
- class Solution {
- public:
- bool ifContain(map<char,int>& source, map<char,int>& target) //check if S contains T
- {
- for(map<char,int>::iterator it = target.begin(); it != target.end(); it++) //map的遍历
- {
- if(source[it->first] < it->second) return false;
- }
- return true;
- }
- string minWindow(string S, string T) {
- int minLength = INT_MAX;
- int start = , end = , minStart = , minEnd = ;
- map<char,int> source;
- map<char,int> target;
- source[S[start]]++; //map的插入[法I]source[key]=value; [法II]source.insert(make_pair(key,value));
- for(int i = ; i< T.length(); i++)
- {
- target[T[i]]++;
- }
- while()
- {
- if(ifContain(source, target)){
- if(end-start+ < minLength)
- {
- minStart = start;
- minEnd = end;
- minLength = end-start+;
- if(minLength == T.size()) return S.substr(minStart,minLength);
- }
- source[S[start]]--; //寻找更小的窗口
- start++;
- }
- else //不包含,则扩大窗口
- {
- end++;
- if(end==S.size()) break;
- source[S[end]]++;
- }
- }
- if(minLength>S.size()) return "";
- else return S.substr(minStart,minLength);
- }
- };
76. Minimum Window Substring (String, Map)的更多相关文章
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- 【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
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- [LeetCode] 76. Minimum Window Substring 解题思路
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [leetcode]76. Minimum Window Substring最小字符串窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 76. Minimum Window Substring(hard 双指针)
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [LeetCode] 76. Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 76. Minimum Window Substring (JAVA)
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [LC] 76. Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
随机推荐
- 集成学习之Boosting —— Gradient Boosting原理
集成学习之Boosting -- AdaBoost原理 集成学习之Boosting -- AdaBoost实现 集成学习之Boosting -- Gradient Boosting原理 集成学习之Bo ...
- Linux 网络编程->epoll<-LT/ET模式整理(~相逢何必曾相识~)
今天自己整理一下epoll,网上有很多经典的介绍,看了很多~收藏了很多~还是整理一下做个积累, 自己的东西好找~ 1. epoll 模型简介 epoll 是Linux I/O 多路复用接口 selec ...
- cool 软件 —— Carnac(实时桌面显示按键)
1. Carnac 下载地址:Carnac, the Magnificent Keyboard Utility 使用说明:carnac – 在屏幕实时显示按键操作
- 戴尔 Latiteude E7240 i7-4600U
一.鲁大师各项数据 二.内存条 三.电池损耗 四.跑分
- UVALive 5135 Mining Your Own Bussiness【tarjan点双】
LINK1 LINK2 题目大意 给你一个无向连通图,让你给一些点染上黑色,需要满足染色之后,断开任意一个节点,要满足任意一个联通块中剩下的节点中至少有一个黑点 思路 一开始想的是把每一个点双联通分量 ...
- L3-013 非常弹的球 (30 分)
刚上高一的森森为了学好物理,买了一个“非常弹”的球.虽然说是非常弹的球,其实也就是一般的弹力球而已.森森玩了一会儿弹力球后突然想到,假如他在地上用力弹球,球最远能弹到多远去呢?他不太会,你能帮他解决吗 ...
- 便捷的Jenkins jswidgets
很多时候我们在构建完成之后需要查看构建的状态,类似github 中的build Status 插件安装 搜索插件 使用 目前好像只支持自由项目的构建 代码集成 <!DOCTYPE html> ...
- 使用UltraISO制作U盘启动
下面给你提供是的一个万能的制作系统U盘的方法,用这个U盘你可以加载任何你想要的系统,即使是Linux系统都是可以,你需要做的就是下载安装软件,下载一个系统安装光盘的镜像文件,然后用软件导入到U盘就可以 ...
- C语言:宽字符集操作函数(unicode编码)
C语言:宽字符集操作函数(unicode编码) 字符分类: 宽字符函数 普通C函数描述 iswalnum() isalnum() 测试字符是否为数字或字母 iswalpha() isalpha() 测 ...
- unittest框架出报告乱码的问题解决
跟着上面的步骤修改好后,unittest断言写法要写成下面这样才能展示非乱码