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 in T in complexity O(n).
For example,
S = “ADOBECODEBANC”
T = “ABC”
Minimum window is “BANC”.
Note:
If there is no such window in S that covers all characters in T, return the empty string “”.
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
分析
在字符串 S 中查找最小的窗口,使窗口中全部包含字符串T 中的字符,(不按顺序)。
需要注意的地方:
- T 中的字符可以重复,窗口需要重复包含。
- 测试 实验中的是SCII字符 ,所以可以用128位数组代替 hash table。
AC代码
class Solution {
public:
string minWindow(string S, string T) {
if (S.empty() || T.empty())
{
return "";
}
int count = T.size();
int require[128] = { 0 };
bool chSet[128] = { false };
for (int i = 0; i < count; ++i)
{
require[T[i]]++;
chSet[T[i]] = true;
}
int i = -1;
int j = 0;
int minLen = INT_MAX;
int minIdx = 0;
while (i < (int)S.size() && j < (int)S.size())
{
if (count)
{
i++;
require[S[i]]--;
if (chSet[S[i]] && require[S[i]] >= 0)
{
count--;
}
}//if
else
{
if (minLen > i - j + 1)
{
minLen = i - j + 1;
minIdx = j;
}
require[S[j]]++;
if (chSet[S[j]] && require[S[j]] > 0)
{
count++;
}
j++;
}//else
}//while
if (minLen == INT_MAX)
{
return "";
}
return S.substr(minIdx, minLen);
}
};
LeetCode(76) Minimum Window Substring的更多相关文章
- LeetCode(5)Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- LeetCode(111) Minimum Depth of Binary Tree
题目 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the s ...
- LeetCode(64) Minimum Path Sum
题目 Total Accepted: 47928 Total Submissions: 148011 Difficulty: Medium Given a m x n grid filled with ...
- LeetCode(76): 最小覆盖子串
Hard! 题目描述: 给定一个字符串 S 和一个字符串 T,请在 S 中找出包含 T 所有字母的最小子串. 示例: 输入: S = "ADOBECODEBANC", T = &q ...
- [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
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- [LeetCode] Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
随机推荐
- canvas draw a image
var c = context.getContext("2d"); var cimg = new Image(); cimg.src = "img path"; ...
- 编译错误:error: multi-line comment
编译错误:error: multi-line comment 这其实是有宏定义的地方的问题. 原因是宏定义非一行,在宏定义的行尾使用 '\' 连接符导致的. 所以这个地方的注释使用 /* */ ...
- nginx+tomcat+memcached-session-manager组成简单的负载均衡和集群
1.搭建环境 192.168.29.128(luxh-01.com) 安装nginx,参考 http://www.cnblogs.com/luxh/p/4067038.html 192.168.29. ...
- javascript笔记:流程控制语句
一.条件语句 1.if语句 if 语句即条件判断语句,一共有三种格式: (1)if (条件表达式) 语句: var box = 100; if (box >50) { alert('box大于5 ...
- js获取浏览器窗口可视区域大小
获得浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)的方法: 一.对于IE9+.Chrome.Firefox.Opera 以及 Safari: • window.innerHeight - 浏 ...
- modelsim(2) - vcd (dump, 查看,格式理解)
二 vcd dump 由于VCD可以用于做功耗分析,所以需要把其dump出来.另外VCD可以作为结果,也可以作为激励,但是实际看到的少啊! VCD是verilog的标准,所以有系统函数$dumpvar ...
- 今天遇到的关于mysql的max_allowed_packet的问题
今天,运维组的同学来找我,说是备份池的文件描述没有显示出来,而且是从20号开始就不能显示,之前的文件描述就能显示,而且20号他们上传备份的数据确实是传过来的.但是是在web界面文件描述显示不出来. 先 ...
- 设置表格td宽度
CSS布局,表格宽度不听使唤的实例.想把表格第一例宽度设为20,其他自适应.但CSS中宽度是等宽的.只设这一行也不起作用.但是在实际应用中总是等宽处理,并不按照样式来走. XML/HTML代码 & ...
- 关于form标签,你该知道
有没有发现,自己在写模板的时候很少使用form元素,一来form和table总是那么傻傻分不清楚:二来form的特性理解不清楚,有了input.label来了直接就上,根本不用form(不知道有没有人 ...
- JS出现illegal character非法字符提示
引用js文件,js文件内的汉字在页面显示乱码 解决方式: a. 保持js文件编码与jsp页面编码格式一致: b. 在引入js文件时,在script中添加charset=""属性,指 ...