[LeetCode] Minimum Window Substring 散列映射问题
题目:
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.
Hash Table Two Pointers String
- T 中的字符可以重复,窗口需要重复包含。
- 测试 实验中的是SCII字符 ,所以可以用128位数组代替 hash table。
- leetcode c++ 中不能用 hash_map。
思路:
- 使用一个映射int need(hash table 或者 128位数组) 存储T中个字符需要的个数,因为需求可能为负,所以用int。
- 使用一个映射bool exitst(hash table 或者 128位数组) 存储T中个字符是否存在,使用hashtable 也构造这个,因为find 是遍历查找的。
- 用一个var 记录窗口中符合T 中char 的个数。
- 用下表start、last 标记窗口位置,start 指向窗口内第一个char,last 指向窗口外右边第一个char(包括结尾)。
- 每次循环加入或删除(记录一个flag)一个字符,如果不存在便继续循环。
- 通过判断加入删除flag进行操作,更新need 表,更新var, 如果等于T 的长度,更新返回记录。
- 循环结束判断能否查找。
#include <string>
#include <hash_map>
#include <iostream>
#include <map>
using namespace std;
using namespace __gnu_cxx; class Solution {
public:
string minWindow(string S, string T) {
int numS = S.length(),numT = T.length();
if(numS<||numT<) return "";
int WinStart=,WinLast=,WinCount =,retStart,leng=INT_MAX;
hash_map<char, int > need;
hash_map<char, bool > exist;
for(int i =;i<numT;i++){
need[T[i]]++;
exist[T[i]] = true;
} bool addorminus;
char curchar;
while(WinStart<=numS-numT){
if(WinCount!=numT&&WinLast<numS){
addorminus = true;
curchar = S[WinLast++];
}
else{
addorminus = false;
curchar = S[WinStart++];
}
if(!exist[curchar]) continue;
if(addorminus){
if(need[curchar]>) WinCount++;
need[curchar]--;
if(WinCount==numT&&leng>WinLast - WinStart){
retStart = WinStart;
leng = WinLast - WinStart;
}
}
else{
if(WinCount==numT&&leng>WinLast - WinStart+){
retStart = WinStart-;
leng = WinLast - WinStart+;
}
need[curchar] ++;
if(need[curchar]>) WinCount--;
}
}
if(leng==INT_MAX)
return "";
return S.substr(retStart,leng);
}
}; int main()
{
string s = "1A123BAC1";
string t = "AABC";
Solution sol; string ret = sol.minWindow(s,t);
cout<<ret<<endl;
return ;
}
class Solution {
public:
string minWindow(string S, string T) {
if (S.empty() || T.empty())
{
return "";
}
int count = T.size();
int require[] = {};
bool chSet[] = {false};
for (int i = ; i < count; ++i)
{
require[T[i]]++;
chSet[T[i]] = true;
}
int i = -;
int j = ;
int minLen = INT_MAX;
int minIdx = ;
while (i < (int)S.size() && j < (int)S.size())
{
if (count)
{
i++;
require[S[i]]--;
if (chSet[S[i]] && require[S[i]] >= )
{
count--;
}
}
else
{
if (minLen > i - j + )
{
minLen = i - j + ;
minIdx = j;
}
require[S[j]]++;
if (chSet[S[j]] && require[S[j]] > )
{
count++;
}
j++;
}
}
if (minLen == INT_MAX)
{
return "";
}
return S.substr(minIdx, minLen);
}
};
[LeetCode] Minimum Window Substring 散列映射问题的更多相关文章
- [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]Minimum Window Substring @ Python
原题地址:https://oj.leetcode.com/problems/minimum-window-substring/ 题意: Given a string S and a string T, ...
- 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] minimum window substring 最小字符窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- LeetCode()Minimum Window Substring 超时,但觉得很清晰。
我的超时思路,感觉自己上了一个新的台阶,虽然超时了,但起码是给出了一个方法. 遍历s 一遍即可,两个指针,当找到了一个合格的字串后,start 开始走,直到遇到s[start]在t中 如果不符合,en ...
- 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 ...
- 【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 ...
- 53. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
随机推荐
- 控制nginx并发链接数量和客户端请求nginx的速率
一.控制nginx并发链接数 ngx_http_limit_conn_module这个模块用于限制每个定义的key值的链接数,特别是单IP的链接数. 不是所有的链接数都会被计数,一个符合计数要求的连接 ...
- phpstudy2016+phpstorm2017-3+xdebug+chrome
1. 勾选Xdebug 后 phpstudy 会自动重启服务 [XDebug] xdebug.profiler_output_dir="D:\phpStudy\tmp\xdebug" ...
- python里字典的用法介绍
一.什么是字典 字典是python里的一种数据类型,特点是元素的无序性,和键key的唯一性.字典的创建方法是{key:values},字典里的键key只能是不可变的数据类型(整型,字符串或者是元组), ...
- uncaught exception 'NSInternalInconsistencyException, reason:[UITableViewController loadView] loaded the "Controller" nib but didn't get a UITableView
http://blog.csdn.net/ryantang03/article/details/7941058#reply 上面那篇文章是我查找的ios实现下拉刷新功能,在我下载完代码运行的过程中发现 ...
- redis--py链接redis【转】
请给原作者点赞--> 原文链接 一.redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链 ...
- selenium2自动处理验证码
最近在研究web自动化,登录时发现要输入验证码,之前在做手机app自动化时,就被验证码block了.这次做web时又遇到了,探索之后,发现有如下几个解决办法: 1.联系开发人员,让其帮忙在测试环境中注 ...
- UVa 12299 线段树 单点更新 RMQ with Shifts
因为shift操作中的数不多,所以直接用单点更新模拟一下就好了. 太久不写线段树,手好生啊,不是这错一下就是那错一下. PS:输入写的我有点蛋疼,不知道谁有没有更好的写法. #include < ...
- luogu2762 太空飞行计划问题
最大权闭合子图 参考这,胡伯涛论文. 10,8,6,3这个简单割对应的闭合子图是A1,B1,B2 输出路径时,最后一次层次图中,与源点相连的点即选做的实验,与汇点相连的点即选用的仪器. #includ ...
- 程序员必需知道的Mac OS使用技巧
macos sierra正式版发布了,于是我把我沉寂了一年没有用过了的macbook拿出来玩玩,顺便把一些常用技巧mark. 1.apple store下载软件无响应(经常出现的问题) 解决方法:更改 ...
- cinema 4d 包括宝典 --- 改线 循环边 建模布线原则
cinema 4d 一.视图控制与物体控制 1.摇移 alt+鼠标左键 转圈看物体 改变角度 2.平移 alt +鼠标中键 不改变角度 移动 3.推拉 alt+鼠标右键 ...