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 ...
随机推荐
- 转: android之虚拟机访问tomcat服务器资源
最近在研究Android虚拟机访问tomcat服务器资源,所以找了个时间写下这篇博客和大家分享一下心得. 其实Android虚拟机访问tomcat服务器非常的简单,只要不要弄错IP地址就可以访问tom ...
- cool 软件 —— Carnac(实时桌面显示按键)
1. Carnac 下载地址:Carnac, the Magnificent Keyboard Utility 使用说明:carnac – 在屏幕实时显示按键操作
- CF1056:Check Transcription(被hack的hash)
One of Arkady's friends works at a huge radio telescope. A few decades ago the telescope has sent a ...
- .NET/C# 使用反射注册事件
使用反射,我们可以很容易地在运行时调用一些编译时无法确定的属性.方法等.那么如何注册事件呢? 本文将介绍如何使用反射注册事件. 本文内容 不使用反射 使用反射 安全地使用反射 参考资料 不使用反射 例 ...
- ringojs 的包管理
ringojs 集成了包管理目前有几种方式 ringo-admin rp ringo-admin 安装包 我们使用ringo-admin 安装rp ringo-admin install grob/r ...
- Outlook中在Exchange服务器无法保存邮件副本
最近帮同事设置Outlook2007,结果她直接登录公司网页Exchange,发现存在Exchange上的邮件副本全没了,原以为是Outlook邮箱账号设置里”保存服务器项副本“没打勾,后来才发现账号 ...
- UEFI +、GPT 、BIOS 、 MBR的关系
1.传统的主板就是传统 BIOS,可在使用 MBR 分区表的硬盘(俗称 MBR磁盘,就是传统常用的模式)上安装32或64位操作系统.同时也支持使用 GUID 分区表的硬盘(俗称GPT磁盘),但该硬盘上 ...
- sql update set使用case when语句
1. update TD_XXXsetdjyzmdm=null,djyzmsj=null,DLCS= case when DLCS is null then 1 else DLCS+1 end whe ...
- (判断)window.open()窗口被关闭后执行事件
$(function() { // start ready var $article_share=$('#body .article').find('li.share'); // $article_s ...
- python 内置方法的时间复杂度
好文,非常值得参考 http://www.orangecube.net/python-time-complexity