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. view抖动效果

    1.使用属性动画 ViewPropertyAnimator.animate(webView).translationX(20).setInterpolator(new CycleInterpolato ...

  2. crosswalk-webview

    https://github.com/crosswalk-project/cordova-plugin-crosswalk-webview https://cordova.apache.org/doc ...

  3. 【Android 系统开发】Android JNI/NDK (三) 之 JNIEnv 解析

    jni.h文件 : 了解 JNI 需要配合 jni.h 文件, jni.h 是 Google NDK 中的一个文件, 位置是 $/Android-ndk-r9d/platforms/android-1 ...

  4. 'Invalid parameter not satisfying: body'

    afnetwork图片上传的时候出错,出现错误 2015-11-09 15:47:59.086 videoPro[3207:132795] *** Assertion failure in -[AFS ...

  5. STL之分配器allocator

    简单介绍下STL中的分配器allocators. allocators我们一般不会直接接触到,甚至可能并不清楚它的存在,简单的来说,它就是一个幕后工作者,我的印象中它的作用主要在于为容器分配一定的空间 ...

  6. PLSQL在64位系统连接不上32位的服务器

    1 主要是因为PLSQL只能接纳32位的客户端 2 下载oracle32位客户端 http://www.oracle.com/technetwork/topics/winsoft-085727.htm ...

  7. Python 之WEB前端插件

    1.Font Awesome ---- 设计字体,图标 2.EasyUI ---- 各种功能 3.JqueryUI ---- 类似EasyUI 4.bootstrap ---- 必须引入JQuery( ...

  8. TJI读书笔记07-初始化

    TJI读书笔记07-初始化 成员初始化 构造方法初始化 初始化块 初始化的顺序 成员初始化 java尽量去保证每个变量在使用前都会得到初始化. 对于方法局部变量,java不会自动初始化他们,如果没有显 ...

  9. webstorm下搭建编译less环境

    webstorm自带less,不过要编译的话需要nodejs环境. 首先去node的主页下载对应版本的nodejs然后安装,下载地址:http://nodejs.org/ 安装完之后打开命令提示符(w ...

  10. nwe

    SELECT  SUBSTR('20150601', 1, 6) AS CALC_MONTH,       CHN.EMPLOYEE_CODE,       CHN.CHANNEL_TYPE,     ...