刷题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 ...
随机推荐
- Linux下使用VsCode进行Qt开发环境搭建
最近在Linux上搞Qt, vim环境还用不太习惯, QtCreator之前使用时莫名其妙崩溃然后丢失代码之后就被我彻底放弃了, 于是研究了一下用VsCode进行Qt开发. 首先是系统环境和下载安装包 ...
- XSStrike工具的安装使用
0x01简介 XSStrike 是一款用于探测并利用XSS漏洞的脚本 XSStrike目前所提供的产品特性: 对参数进行模糊测试之后构建合适的payload 使用payload对参数进行穷举匹配 内置 ...
- 发布开源项目到Jcenter
前言 为了将阿里云短信开箱即用发布到Jcenter仓库,前前后后花费了1天半的时间,把端午节都搭进去了.终于今天收到了Jcenter的消息,自己发布的包被添加到了Jcenter仓库,也算给开源社区做了 ...
- 【python基础语法】第4天作业练习题
""" 有6道题(通过字典来操作): 1. 某比赛需要获取你的个人信息,设计一个程序, 运行时分别提醒输入 姓名.性别.年龄 ,输入完了,请将数据存储为一个字典, 2.数 ...
- Pikachu-XSS(跨站脚本)
XSS(跨站脚本)概述 Cross-Site Scripting 简称为“CSS”,为避免与前端叠成样式表的缩写"CSS"冲突,故又称XSS.一般XSS可以分为如下几种常见类型: ...
- HDU1408 - 盐水的故事
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1408 解题思路:主要考虑最后一滴可能不满足D毫升,但仍算1秒.另外还要注意浮点数的比较. #inclu ...
- 剑指offer-基础练习-快速排序-排序
/* 题目:快速排序 */ /* 思路:将一个数组分为两份,左边的数字小于index,右边的数字大于index,递归划分后形成一个排序后的数组. */ void QuickSort(int data[ ...
- PAT (Advanced Level) Practice 1055 The World's Richest (25 分) (结构体排序)
Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...
- Oracle 中关于 Group By 子句与多行函数嵌套搭配使用的注意事项
目录 你需要知道的 啥叫单行函数 啥叫多行函数 如何理解这个概念 Group by 子句使用规则 看一道 071 考题 你需要知道的 提到 Group by 子句,你需要先理解一个东西:函数的分类.提 ...
- http断点续传Range与Content-Range
今天用别人封装的libcurl库下载文件,发现下载下来的文件总是缺少头两个字节,用以下配置启用HTTP头信息打印后发现原来是设置了断点续传位置的原因 curl_easy_setopt(m_pCurl, ...