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 ...
随机推荐
- Zero Requiem
“最后是在游行.暴君鲁路修高居王座,两侧列着所有反对者的代表:黑色骑士团.黎星刻.原圆桌骑士名列第三的吉诺,以及一身女囚装的娜娜丽,他们都即将被公开处死.尤菲米娅在第一次“特别行政区•日本”成立仪式上 ...
- DestroyWindow函数注意事项
最近遇到这样一个问题:将一个窗口句柄以参数的形式传递给一个线程,在线程中使用完之后要将窗口销毁,调用DestroyWindow销毁窗口是返回false,GetLastError的结果为5:拒绝访问,而 ...
- JNI环境搭建,CDT, cygwin,NDK
1.为eclipse增加c和c++的开发插件 下载地址:http://www.eclipse.org/cdt/downloads.php 2,安装cygwin Ndk需要运行在linux环境下,cyg ...
- MyBatis环境配置
<settings> <!-- 使全局的映射器启用或禁用缓存. --> <setting name="cacheEnabled" value=&quo ...
- JAVA 新手问题: Request 编码编译出错,Unhandled exception type UnsupportedEncodingException
新手: 编写如下代码 private void Exec(HttpServletRequest Req,HttpServletResponse Response) //throws ServletEx ...
- attr与prop
Jquery获取checkbox属性checked为undefined (-- ::)转载▼ 标签: js jquery checkbox checked undefined 分类: JQuery 使 ...
- 抽象类和抽象方法(关键字abstract)
1.抽象类不能被实例化,可以没有,一个或多个抽象方法 2.抽象方法只有方法的声明但没有方法的实现,有抽象方法的类必须声明为抽象类,子类必须重写父类所有的抽象方法才能被实例化,否则子类也是个抽象类, ...
- C++设计模式-Builder建造者模式
作用:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. Builder模式和AbstractFactory模式在功能上很相似,因为都是用来创建大的复杂的对象,它们的区别是:B ...
- '<', hexadecimal value 0x3C, is an invalid 问题解决
你的web.config 里面一定有个节点的不完整,如 错误如下: 正确的如下:
- JS函数arguments数组获得实际传参数个数
JS与PHP在函数传参方面有点不同,PHP形参与实参个数要匹配,而JS就灵活多了,可以随意传参,实参比形参少或多都不会报错. 实参比形参多不会报错 ? 1 2 3 4 5 function say(a ...