刷题76. Minimum Window Substring
一、题目说明
题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n)。难度是Hard!
二、我的解答
先说我的思路:
(1)统计t中每个字符出现的次数,
(2)用hash存储s中出现t中字符的位置,
(3)计算最短字符串。
思路有了,惭愧的是,这个题目,我没做出来。
后来参考大神的实现了,用“滑动窗口”实现。
代码如下:
class Solution{
public:
string minWindow(string s,string t){
if(s.empty() || t.empty()) return "";
int left=0,right=0;
string res = s;
int minLen = INT_MAX;
int start = 0;
unordered_map<char,int> window;//当前「窗口」中包含的字符及出现的次数
unordered_map<char,int> needs;//t 中包含的字符及出现次数
//统计字符串t中各个字符的数量
for(char ch:t){
needs[ch]++;//如果该 key不存在,C++ 会自动创建这个 key,并把 map[key] 赋值为 0
}
int match = 0;//记录 window 中已经有多少字符符合要求了
while(right<s.size()){
char c1 = s[right];
if(needs.count(c1)){
window[c1]++;
if(window[c1]==needs[c1]){
match++;//字符 c1 的出现次数符合要求了
};
}
right++;
//window 中的字符串已符合 needs 的要求了
while(match==needs.size()){
//缩减res
if(right-left<minLen){
start = left;
minLen = right - left;
}
char c2 = s[left];
if(needs.count(c2)){
window[c2]--;
if(window[c2]<needs[c2]){
match--;
}
}
left++;
}
}
return minLen == INT_MAX ? "" : s.substr(start, minLen);
}
};
性能一般:
Runtime: 28 ms, faster than 45.63% of C++ online submissions for Minimum Window Substring.
Memory Usage: 10.2 MB, less than 68.00% of C++ online submissions for Minimum Window Substring.
三、优化措施
我消化消化再说。
刷题76. Minimum Window Substring的更多相关文章
- 【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
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- [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]76. Minimum Window Substring最小字符串窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- lintcode 中等题:minimum window substring 最小子串覆盖
题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...
- 【一天一道LeetCode】#76. Minimum Window Substring
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 76. Minimum Window Substring(hard 双指针)
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 76. Minimum Window Substring (JAVA)
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
随机推荐
- 03.JS运算符
前言: 学习一门编程语言的基本步骤 (01)了解背景知识 (02)搭建开发环境 (03)语法规范 (04)常量和变量 (05)数据类型 (06)数据类型转换 (07)运算符7.运算符 表达式:由运 ...
- MySQL数据库root密码忘记丢失重置方法
对于DBA来说,丢失超管用户root的密码是致命的, 可以通过添加--ship-grant-tables参数来跳过权限表. 1.忘记root密码,进不去数据库: 这个时候需要强制停库,先查看MySQL ...
- C# 二进制 十进制 十六进制 之间的转换
; Console.WriteLine(a.ToString("X")); //10进制转16进制 Console.WriteLine(Convert.ToString(a, )) ...
- Eclipse安装WebJavaEE插件、Eclipse编写HTML代码(综合问题统一记录)
1 Eclipse没有Web插件和JavaEE插件咋整 1.1 在Eclipse中菜单help选项中选择install new software选项 1.2 在work with 栏中输入 http: ...
- WAF的基础绕过
方法分类: 1.HTTP参数污染绕过 2.HTTP Header头部欺骗绕过 3.HTTP参数溢出绕过 4.HTTP分块传输绕过 5.HTTP数据编码绕过 6.HTTP Pipline绕过(Keep- ...
- kubernetes安装-二进制
主要参考https://github.com/opsnull/follow-me-install-kubernetes-cluster,采用Flanel和docker 系统信息 角色 系统 CPU C ...
- 将CGCS2000的坐标值转换为WGS84的坐标值
打开图层数据,或者将已有的Excel数据导入到ArcMap中,然后打开Toolbox, ArcToolbox --> Projections and Transformations --> ...
- C#简单的LogHelper
适用于不想使用log4net等第三方的Log工具的LogHelper.正规的还是要使用<C# 工具类LogHelper>的这种做法. using System; using System. ...
- Object的对象的相关方法
Object.getPrototypeOf() Object.getPrototypeOf方法返回参数对象的原型.这是获取原型对象的标准方法. var F = function () {}; var ...
- mnist识别优化——使用新的fashion mnist进行模型训练
今天通过论坛偶然知道,在mnist之后,还出现了一个旨在代替经典mnist数据集的Fashion MNIST,同mnist一样,它也是被用作深度学习程序的“hello world”,而且也是由70k张 ...