刷题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 ...
随机推荐
- #《Essential C++》读书笔记# 第五章 面向对象编程风格
基础知识 继承机制定义了父子(parent/child)关系.父类(parent)定义了所有子类(children)共通的共有接口(public interface)和私有实现(private imp ...
- 8maven配置多个项目之间的依赖
首先创建两个项目进行测试依赖 创建一个HelloWorld2项目,一个HelloWorld类里面有一个sayHello的方法 然后再创建一个HelloWorldTime项目,一个SayHelloWor ...
- cf1176D
题意简述:数组a经过一系列操作之后获得数组b,给你数组b,构造出一个满足条件的数组a 操作如下从左到右扫描数组a,如果是一个素数,那么把第这个素数的素数加到数组a中,例如a[1]=2那么加3到数组a当 ...
- 高并发系统:消息队列MQ
注:前提是知道什么是消息队列.不懂的去搜索各种消息队列入门(activeMQ.rabbitMQ.rocketMQ.kafka) 1.为什么要使用MQ?(MQ的好处:解耦.异步.削峰) (1)解耦:主要 ...
- 【笔记】机器学习 - 李宏毅 - 11 - Keras Demo2 & Fizz Buzz
1. Keras Demo2 前节的Keras Demo代码: import numpy as np from keras.models import Sequential from keras.la ...
- 关于新版vue-cli安装json-server在build文件里没生成出dev-server文件
今天在安装json-server时遇到一个问题,build文件里并没有生成dev-server.js文件, 开始是怀疑配置有问题,或者安装不正确,然后重新安装了两三次,还是这样,郁闷.. 通过查询资料 ...
- CentOS8中进行IP和主机名的网络配置的过程图解
摘要: 很多人不知道如何在字符界面下配置主机名和ip,所以写了这个文章,本人也是新手,希望指出错误与不足.(本文只是在字符界面下教程) 一.输入你的账号密码登录 1)ifconfig 查看你目前的 主 ...
- 限定输入框只能输入数字, TextBox的TextChanged事件调用
/// <summary> /// 限定输入框只能输入数字, TextBox的TextChanged事件调用 /// </summary> /// <param name ...
- ASP.NET MVC自定义Numberic属性的验证信息
最近在使用MVC4时碰到一个Model验证的问题:整型属性输入非整型字符串时,错误信息总是“字段 XXX 必须是一个数字”,我总觉得这句话读起来很别扭,所以就萌生了要改变这个默认错误提示信息的念头,但 ...
- Python异常类型及包含关系
Python异常类型及包含关系,设计异常捕获时参考: BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- ...