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 ...
随机推荐
- bootstrap-列表组
<div class="container"> <!-- list-group 列表组 给ul添加 list-group-item 列表项 给li添加 --> ...
- Java集合---LinkedList源码解析
一.源码解析1. LinkedList类定义2.LinkedList数据结构原理3.私有属性4.构造方法5.元素添加add()及原理6.删除数据remove()7.数据获取get()8.数据复制clo ...
- finder文件目录跳转快捷键
finder文件目录跳转快捷键 command+shift+G
- CSS3 初步学习
CSS3有一些是与旧版CSS2.1重叠的,有一些是没有浏览器支持的,全学没必要,下面只记录一下有用的. 一.CSS3边框 1.圆角border-radius border-radius:值越大,角越圆 ...
- NHibernate系列文章十二:Load/Get方法
摘要 NHibernate提供两个方法按主键值查找对象:Load/Get. 1. Load/Get方法的区别 Load: Load方法可以对查询进行优化. Load方法实际得到一proxy对象,并不立 ...
- maven自动部署到远程tomcat教程
使用maven的自动部署功能可以很方便的将maven工程自动部署到远程tomcat服务器,节省了大量时间. 本文章适用于tomcat的7.x ,8.x, 9.x版本. 下面是自动部的步骤 1,首先,配 ...
- sqlserver 加内置dll的使用内存
- nginx环境下配置nagiosQL-关于nagiosql配置文件
接上文:nginx环境下配置nagios-关于nginx.conf nagiosql文件应该处于conf/domain/目录下 nagiosql配置如下: ; gzi ...
- ngx.lua中遇到的小问题
作者: 胡 志伟 分类: ngx_lua, 开发代码 发布时间: 2013-09-26 08:40 ė 6评论关闭 在使用ngx.redirect 到一个新的地址时,错误日志提示: lua entry ...
- 详解Oracle DELETE和TRUNCATE 的区别(摘)
语法delete from aa truncate table aa 区别 1.delete from后面可以写条件,truncate不可以. 2.delete from记录是一条条删的,所删除的每行 ...