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! 二.我的解答 先说我的思路: ...
随机推荐
- java io 输入输出流
数据流分类: 流序列中的数据既可以是未经加工的原始二进制数据, 也可以是经一定编码处理后符合某种格式规定的特定数据. 因此Java中的流分为两种: 1) 字节流:数据流中最小的数据单元是字节 2) 字 ...
- 根据 MySQL 状态优化 ---- 1. 慢查询
查看 MySQL 服务器运行的各种状态值: mysql> show global status: 1. 慢查询 mysql> show variables like '%slow%'; + ...
- Capture a Screen Shot
using System; using System.Runtime.InteropServices; using System.Drawing; using System.Drawing.Imagi ...
- POJ C Looooops
Description A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; ...
- revealapp 用于调试IOS的UI
下载地址(官网):http://revealapp.com/ 破解方法:http://blog.csdn.net/ljb_wh/article/details/39345599
- window7下使用vagrant打造lamp开发环境(一)
前言: 公司电脑只有win7 + (xampp || wamp)开发,处于个人爱好,想学习下在最舒服的开发环境下开发,也不想安装双系统,想用Vmware电脑却配置不高,vagrant+virtual ...
- devexpress中如何给TabPage控件的Tab定义背景色
js: /*tab选项卡样式*/ .color .dxtc-link { background-color: #bf4e6a !important; } 后台代码: //选项卡样式 protected ...
- ASP.Net将图片以二进制方式存入数据库,并读取
把图片转换成二进制--把二进制转换成图片 private void button1_Click(object sender, EventArgs e) { string path = this.tex ...
- 2016-06-08:Windows中的bat脚本
涉及循环嵌套,启用变量延时,算术运算 @echo off setlocal enabledelayedexpansion %路径以及文件名等变量设置% set x264_exe=E:\demo\c++ ...
- 【转】Using Gamma 2.2
This is a detailed description of the work with Gamma 2.2. If you are only interested in exact instr ...