一、题目说明

题目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的更多相关文章

  1. 【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 ...

  2. 76. Minimum Window Substring

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. lintcode 中等题:minimum window substring 最小子串覆盖

    题目 最小子串覆盖 给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串. 样例 给出source = "ADOBECODEBANC ...

  7. 【一天一道LeetCode】#76. Minimum Window Substring

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. python选课系统作业

    # 选课系统# 角色:学校.学员.课程.讲师# 要求:# 1. 创建北京.上海 2 所学校# 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海 ...

  2. C#实现图片文件到数据流,再到图片文件的转换

    //----引入必要的命名空间 using System.IO; using System.Drawing.Imaging; //----代码部分----// private byte[] photo ...

  3. opencv —— copyTo 设置与操作感兴趣区域(ROI)

    感兴趣区域:ROI 对感兴趣区域进行的一系列操作,相当于直接在原图相应部分进行操作. Mat imageROI = srcImage(Rect(0,0,dstImage.cols, dstImage. ...

  4. [大数据技术]Kettle从CSV文件读取清洗后到MySQL中文乱码问题

    首先要知道CSV文件的编码格式 然后在文件输入编码选择编码格式, 第二步,在每个转换或者作业的DB连接中选择选项,并添加如下内容: 中文乱码问题得到解决

  5. SpringBoot导出excel数据报错Could not find acceptable representation

    转自:https://blog.csdn.net/mate_ge/article/details/93518286?utm_source=distribute.pc_relevant.none-tas ...

  6. C# Socket的方式发送中文,接收方收到乱码

    场景: 使用 Socket的方式向下位机发送中文信息,下位机收到的中文是乱码 原因: 了解到的原因是上位机与下位机的发送与接收的编码与解码方式不一致 比如上位机采用 Encoding.UTF8.Get ...

  7. 松软科技课堂:jQuery 效果 - 滑动

    jQuery 滑动方法 通过 jQuery,您可以在元素上创建滑动效果. jQuery 拥有以下滑动方法: slideDown() slideUp() slideToggle() jQuery sli ...

  8. 使用 Hyper-V 替代 VMware

    目前我又用回了vmware,原因是,Hyper-V性能不如vmware,而且导出的虚拟机文件,再导入会出现奇怪的问题.不省心. Hyper-V是什么 Hyper-V硬件要求为Windows 10 企业 ...

  9. P3759 [TJOI2017]不勤劳的图书管理员 [树套树]

    树套树是什么啊我不知道/dk 我只知道卡常数w // by Isaunoya #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC o ...

  10. Codeforces Round #592 (Div. 2) D - Paint the Tree

    题目链接:https://codeforces.com/contest/1244/problem/D 题意:给你一个树,让你把树上的每个节点染成三种颜色,使得任意三个互相相邻的节点颜色都不一样(意思是 ...