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 ...
随机推荐
- java- Collection Map集合
package map; import java.util.Collection; import java.util.HashMap; import java.util.Map; import jav ...
- Git 之 git原理简介
这里只是很简单.超简单的介绍下git,为的是方便记忆: 本地仓库分为三个部分:工作区.暂存区.仓库区,其中暂存区和仓库区属于版本区. 对于文件的操作,需要从工作区----> 暂存区 ----&g ...
- 程序级的AOP到底好不好?
很多年前模拟过Spring的AOP机制,简单的实现其实不难,但真正要保证切入代码符合预期的设计,不会引起负面影响,特别是要保证原来逻辑的稳定性,即AOP的强壮性.个人感觉还是很难,如果横切的代码过多, ...
- 关于EPoll的个人理解
1.epoll 是I/o多路复用的一种解决方案,对比select的优点有: a.支持打开最大的文件描述符(可高达百万) b.效率并不随着描述符的增多而线性下降.select每次是轮询,所以描述符越多效 ...
- Qt UI界面改了,但UI界面不更新
/**************************************************************************** * Qt UI界面改了,但UI界面不更新 * ...
- IOS中UITableViewCell使用详解
IOS中UITableViewCell使用详解 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(N ...
- Java与WCF交互(二):WCF客户端调用Java web service【转】
原文:http://www.cnblogs.com/downmoon/archive/2010/08/25/1807982.html 在上篇< Java与WCF交互(一):Java客户端调用WC ...
- USB学习笔记-总结
1. # ls /sys/bus/usb/devices/解析:1-0:1.0 1-1 1-1:1.0 2-0:1.0 2-1 2-1:1.0 2-2 2-2.1 2-2:1.0 2-2.1:1.0 ...
- Spring AOP 实现读写分离
原文地址:Spring AOP 实现读写分离 博客地址:http://www.extlight.com 一.前言 上一篇<MySQL 实现主从复制> 文章中介绍了 MySQL 主从复制的搭 ...
- Intellij Idea上Spring Boot编译报错:Error:(3, 32) java: 程序包org.springframework.boot不存在
很尴尬,为了使用Spring Boot的Initializr,特意下了个Intellij Idea,刚按提示新建一个Spring Boot的Maven项目后,就出现红叉叉了.因为IDE是新的,开始是M ...