[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 ...
随机推荐
- 从屏幕截取一块区域,将其赋给imageView
UIGraphicsBeginImageContext(self.bounds.size); [self.layerrenderInContext:UIGraphicsGetCurrentContex ...
- Where art thou-freecodecamp算法题目
Where art thou 1.要求 写一个 function,它遍历一个对象数组(第一个参数)并返回一个包含相匹配的属性-值对(第二个参数)的所有对象的数组. 如果返回的数组中包含 source ...
- 对象、句柄、ID之间的区别
对象是C++的概念,C++的类对象 句柄是Windows SDK的概念,指向某种资源的一种“指针”(有时候底层不一定是指针) 资源ID在MFC里仅仅是一个宏,也就是个整数. 其实,句柄是控件在数据结构 ...
- 01将图片嵌入到Markdown文档中
将图片内嵌入Markdown文档中 将图片嵌入Markdown文档中一直是一个比较麻烦的事情.通常的做法是将图片存入本地某个路径或者网络存储空间,使用URL链接的形式插入图片: ![image][ur ...
- php过滤html标签
<?php function kill_html($str){ //清除HTML标签 $st=-1; //开始 $et=-1; //结束 $stmp=array(); $stmp[]=" ...
- DeepFaceLab小白入门(5):训练换脸模型!
训练模型,是换脸过程中最重要的一部分,也是耗时最长的一部分.很多人会问到底需要多少时间?有人会告诉你看loss值到0.02以下就可以了.我会告诉你,不要看什么数值,看预览窗口的人脸.看第二列是否和第一 ...
- Huawei warns against 'Berlin Wall' in digital world
From China Daily Huawei technologies criticized recent registration imposed on the Chinese tech comp ...
- Python爬虫一
爬虫 什么是爬虫? 网络爬虫(又被称为网页蜘蛛,网络机器人)就是模拟客户端发送网络请求,接收请求响应, 一种按照一定的规则,自动地抓取互联网信息的程序. 原则上,只要是浏览器(客户端)能做的事情,爬虫 ...
- 《流畅的python》读书笔记,第一章:python数据模型
这本书上来就讲了魔法方法,也叫双下方法.特殊方法,通过两个例子对让读者了解了双下方法的用法,更重要的是,让我一窥Python的语言风格和给使用者的自由度. 第一个例子:一摞Python风格的纸牌: i ...
- 排序 sort函数
sort函数见下表: 函数名 功能描述 sort 对给定区间所有元素进行排序 stable_sort 对给定区间所有元素进行稳定排序 partial_sort 对给定区间所有元素部分排序 partia ...