LeetCode OJ--Minimum Window Substring ***
https://oj.leetcode.com/problems/minimum-window-substring/
模拟题
这道题细节比较多。从左到右扫一遍模拟着做
class Solution {
public:
string minWindow(string S, string T) {
string ans = "";
if(S.size() < T.size() )
return ans;
unordered_map<char,int> count;
unordered_set<char> charInT;
unordered_map<char,int> countT;
for(int i = ; i < T.size(); i++)
{
charInT.insert(T[i]);
countT[T[i]]++;
}
int ansI = , ansJ = ;
// 先找第一个合法的
for(int i = ; i < S.size(); i++)
{
if(charInT.find(S[i]) != charInT.end())
{
count[S[i]]++;
// 如果都找到了
if(count.size() == countT.size())
{
bool flag = true;
for(unordered_map<char,int>::iterator itr = countT.begin(); itr != countT.end(); itr++)
{
if(itr->second > count[itr->first])
flag = false; // 还没都匹配
}
if(flag)
{
ansJ = i;
ans = S.substr(ansI,ansJ+);
break;
}
}
}
}
// 往后遍历
for(int m = ; m < S.size(); m++)
{
if(charInT.find(S[m]) == charInT.end())
ansI++; // 往前走1个是安全的
else
{
count[S[m]]--;
if(count[S[m]] >= countT[S[m]])
ansI++;
else
{
if(ans.size() > ansJ - m + )
ans = S.substr(m,ansJ - m +);
// find new end
int temp = ansJ;
temp++;
while(temp<S.size() && S[temp] != S[m])
{
if(charInT.find(S[temp]) != charInT.end())
count[S[temp]]++; // 记录新加进来了合法的
temp++;
}
if(temp == S.size()) // 到了最后也没找到
{
return ans;
}
else
{
ansJ = temp;
count[S[temp]]++;
}
}
}
}
}
};
LeetCode OJ--Minimum Window Substring ***的更多相关文章
- [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 ...
- [Leetcode][JAVA] Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- Java for LeetCode 076 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 ...
- 【leetcode】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
原题地址 用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符 1. 扩展窗口.向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移 2. 收缩窗口.向右调整 ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- 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 ...
随机推荐
- Newtonsoft.Json(Json.net)的基本用法
Newtonsoft.Json(Json.net)的基本用法 其它资料 JSON详解 添加引用: 使用NuGet,命令:install-package Newtonsoft.Json 实体类: pub ...
- MyBatis Sql语句中的转义字符
1.在xml的sql语句中,不能直接用大于号.小于号要用转义字符 如果用小于号会报错误如下: org.apache.ibatis.builder.BuilderException: Error cre ...
- UNIX网络编程-send、recv、sendto、recvfrom详解
send.recv和sendto.recvfrom,一般情况下,send.recv在TCP协议下使用,sendto.recvfrom在UDP协议下使用,也可以在TCP协议下使用,不过用的很少. 1.s ...
- c/c++运算顺序问题
发现没弄清楚这个问题的人很多,连我们c++老师都没弄清楚,转载一篇文章,以及C++ Primer原文. 裘宗燕:C/C++ 语言中的表达式求值 经常可以在一些讨论组里看到下面的提问:“谁知道下面C语句 ...
- Codeforces Round #228 (Div. 1) A
A. Fox and Box Accumulation time limit per test 1 second memory limit per test 256 megabytes input s ...
- mac配置vim-go
基本的设置信息(参考网址:http://hessian.cn/p/1026.html): "还是配置/.vimrc文件. syn on "语法支持 set laststatus=2 ...
- uva 10340 All in All
水题,从头到尾扫一遍就可以了,输入两个字符串s和t,判断是否可以从t中删除0个或多个字符(其他字符顺序不变),得到字符串s.例如,abcde可以得到bce,但无法得到dc. #include<a ...
- 浅谈OpenGL变换矩阵
OpenGL中使用的矩阵全为列向量为主的矩阵. 参考OpenGL变换网站为 http://www.songho.ca/opengl/gl_transform.html 1.什么是GL_MODELVI ...
- java 中小数点的处理
第一种 BigDecimal bg = new BigDecimal(f); double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleVa ...
- C++中关于string类型究竟能不能用cout输出的问题
先让我讲下故事哈 一次在MFC中用cout输出一个string类型字符串,编译时出现这样一个错误: error C2679: binary '<<' : no operator defin ...