leetcode76. Minimum Window Substring
leetcode76. Minimum Window Substring
题意:
给定字符串S和字符串T,找到S中的最小窗口,其中将包含复杂度O(n)中T中的所有字符。
例如,
S =“ADOBECODEBANC”
T =“ABC”
最小窗口为“BANC”。
注意:
如果S中没有覆盖T中所有字符的窗口,返回空字符串“”。
如果有多个这样的窗口,您可以确保在S中始终只有一个唯一的最小窗口。
思路:
一个right指针遍历s,每次遇到t中的字符,在map中减少一个,同时用一个count做统计,当t中所有字符被遍历的时候,做一次统计,并且将left指针移动,直到count != t.length() ,相当于一个窗口在s字符串上配合map表动态滑动。
ac代码:
C++
class Solution {
public:
string minWindow(string s, string t) {
int map[256] = {0};
bool hash[256] = {false};
int slen = s.length();
int tlen = t.length();
if(!slen || !tlen) return "";
int left,right,res,cnt,minleft;
minleft = cnt = left = right = 0;
res = slen + 1;
for(char ch:t)
{
map[ch]++;
hash[ch] = true;
}
while(right < slen)
{
if(hash[s[right++]] && --map[s[right - 1]] >= 0)
cnt++;
while(cnt == tlen)
{
if(right - left < res)
{
res = right - left;
minleft = left;
}
if(hash[s[left++]] && ++map[s[left - 1]] > 0 )
{
cnt--;
}
}
}
if(res > slen) return "";
return s.substr(minleft, res);
}
};
python
leetcode76. Minimum Window Substring的更多相关文章
- 53. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- 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 ...
- 【LeetCode】76. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- [Swift]LeetCode76. 最小覆盖子串 | 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 最小窗口子串
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 ...
随机推荐
- 使用maven打包项目遇到错误: http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
今天在使用maven打包项目时遇到一个错误: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin ...
- 做Mysql主从时,注意使用replicate_wild_do_table和replicate-wild-ignore-table【转】
做Mysql主从时,注意使用replicate_wild_do_table和replicate-wild-ignore-table 浓缩版: 使用replicate_do_db和replicate_i ...
- logging模块配置笔记
logging模块配置笔记 log文件的路径 #判断在当前的目录下是否有一个logs文件夹.没有则创建 log_dir = os.path.dirname(os.path.dirname(__file ...
- python requests模块手动设置cookies的几种方式
def use_cookie(self): cookies="YF-V5-G0=731b77772529a1f49eac82a9d2c2957f; SUB=_2AkMsEgief8NxqwJ ...
- Python抽象类和接口类
一.抽象类和接口类 继承有两种用途: 一:继承基类的方法,并且做出自己的改变或者扩展(代码重用) 二:声明某个子类兼容于某基类,定义一个接口类Interface,接口类中定义了一些接口名(就是函数名) ...
- oracle客户端不需要配置tnsnames.ora文件直接连接服务器数据库
在以前的oracle使用过程中,想要在客户端连接到服务器时,都是在客户端中的tnsnames.ora文件配置如以下内容: adb = (DESCRIPTION = (ADDRESS_LIST = (A ...
- Python语言库pyttsx3
这是一个文字转语音的python模块. 1. macos下安装的时候出现问题: 后来发现,Foundation, AppKit, PyObjCTools都不存在,主要原因是缺少依赖模块pyobjc, ...
- apache 软件历史版本查询
http://archive.apache.org/dist/ http://archive.apache.org/dist/struts/binaries/
- python和C语言的差别
之前在公司一直做的是C语言的开发,然后做的都是业务方面的东西,做的是sdk,因为最近在找工作,然后今天面试的时候被问到C语言和Python的区别,自己只是简单的说了C是静态语言,在变量在使用前进行声明 ...
- git配置用户名跟邮箱
因为我有两个git账号 所以我现在要改变我的默认用户名跟邮件 我就需要去终端设置用户名跟邮箱 具体的命令行就是 设置git的用户名 git config --global user.name &quo ...