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. Maven依赖排除 禁止依赖传递 取消依赖的方法

    大家都知道Maven的优点是依赖管理,特别是前期使用ANT的开发者都有很多感触.最近要开发一个java工程,定的要使用maven,会使用hadoop和hbase的客户端,而引入一个hadoop-cli ...

  2. 65. Reverse Integer && Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, re ...

  3. Monty Hall Problem的一个图解,感觉不错

    从Coursera.org上的台大概率课讨论组里拿来的 如果不转换,选中汽车的概率是1/3,非常显然. 但转换后选中汽车的概率变成2/3就有点反直觉了,并不是太容易想明白. 因为转换其实有4种:汽车- ...

  4. Show "Appear Offline" in Lync

    在注册表中 HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Communicator 创建DWORD,name是EnableAppearOffline,v ...

  5. php把错误日志输入到文件里。

    display_errors = On 开启状态下,若出现错误,则报错,出现错误提示 dispaly_errors = Off 关闭状态下,若出现错误,则提示:服务器错误.但是不会出现错误提示 log ...

  6. 基于boa服务器的web控制mini2440的GPIO口

    win7 系统  虚拟机:ubuntu12.04 开发板:mini2440 上一篇已经详细的讲解了如何配置boa服务器,在这里我们就要利用boa服务器带来的便利,利用web控制开发板上的GIPO口,这 ...

  7. oracle client与ODAC的字符集

    1.pl/sql developer 9里检查客户端字符集与服务端是否一致 首选项,选项,检查客户机与服务器字符集是否匹配 2.Windows环境变量的修改即时生效 3.ODAC12安装后字符集的变化 ...

  8. JavaScript Function(函数表达式)

    创建函数 创建函数的方式有两种:1.函数声明,2.函数表达式 函数声明的语法为 functionName(); //不会报错,函数声明提升function functionName(arg0,arg1 ...

  9. iOS 原生态扫描二维码、条形码的功能。

    1.导入AVFoundatin.framework. 2.新建一个viewController,命名为QRScanViewController,用于扫描的界面. h文件如下,设置代理. #import ...

  10. PHPDocument 代码注释规范总结

    PHPDocument 代码注释规范 1. 安装phpDocumentor(不推荐命令行安装)在http://manual.phpdoc.org/下载最新版本的PhpDoc放在web服务器目录下使得通 ...