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 ***的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. 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 ...

  5. [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 ...

  6. 【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 ...

  7. Leetcode#76 Minimum Window Substring

    原题地址 用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符 1. 扩展窗口.向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移 2. 收缩窗口.向右调整 ...

  8. [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof  ...

  9. Minimum Window Substring @LeetCode

    不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...

  10. 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 ...

随机推荐

  1. Spark ML 文本的分类

    最近一直在研究Spark的分类算法,因为我们是做日志文本分类,在官网和各大网站一直没找到相应的Demo,经过1个多月的研究,终于有点成效. val sparkConf = new SparkConf( ...

  2. Linux 下没有conio.h 已解决

    原文:http://blog.sina.com.cn/s/blog_6a95e00b0100zqvf.html #include <stdio.h>//#include <conio ...

  3. MYSQL C API : struct MYSQL_STMT 结构的组合使用

    #include <iostream> #include <string> #include <string.h> #include <assert.h> ...

  4. go 的 protoc 插件调用逻辑

    要让protoc使用插件,需要做下面事情: Place the plugin binary somewhere in the PATH and give it the name "proto ...

  5. CCF NOI系列活动

    NOI-全国青少年信息学奥林匹克竞赛全国青少年信息学奥林匹克竞赛(NOI)是国内信息学领域内面向中学生的最高水平的大赛,每省派经选拔产生的选手(其中一名是女选手)参加,NOI每年在不同的省市举行. N ...

  6. 【知识点】安全存储服务(SSS)目标应用程序类型

    存在若干种类型的目标应用程序.它们分为两大类:单独的目标应用程序和组目标应用程序.目标应用程序的类型与用于映射用户凭据的帐户类型对应.如果每个用户都在目标应用程序中具有一个帐户,则选择单独的类型.如果 ...

  7. 使用AJAX完成用户名是否存在异步校验

    一.JSP代码: 1.事件触发:onblur 2.编写AJAX代码:向Action中提交,传递username参数 <script> function checkUsername(){ / ...

  8. C++引用与指针

    在做函数参数时, 引用不可以设置默认值, 指针可以 void fun(const string& url,  string* domain = NULL); 另const放在函数后面, 表示这 ...

  9. php数组转换成json格式。

    { "touser":"OPENID", "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0t ...

  10. Thinkphp源码分析系列(一)–入口文件

    正如官方文档上所介绍的,thinkphp使用单一入口,所有的请求都从默认的index.php文件进入.当然不是说一定非得从index.php进入,这应该取决于你的服务器配置,一般服务器都会有默认的首页 ...