一、题目说明

题目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. IIS网站部署配置

    1.配置Session State

  2. #《Essential C++》读书笔记# 第六章 以template进行编程

    练习题答案 练习6.1 试改写以下类,使它成为一个class template: class example { public: example(double min, double max); ex ...

  3. WebStorm 2019.3.1 永久破解

    PS:动手能力强的来,手残的去淘宝买吧,大概15块钱1年.建议看完后在动手,有一个全局观,浪费不了多少时间 一. 下载破解补丁文件 链接:https://pan.baidu.com/s/16-rPPH ...

  4. 前端调用本地摄像头实现拍照(vue)

    由于调用摄像头有使用权限,只能在本地运行,线上需用https域名才可以使用. <template> <div class="camera_outer"> & ...

  5. 【IDE】IDEA - 使用问题记录

    1.显示方法API:类似eclipse把鼠标放到方法上有api信息. setting/keymap中搜索 quick docs:(有说默认快捷键是ctrl+q,但我的不是待查找快捷键) 2.forea ...

  6. 使用TableHasPrimaryKey或TableHasForeignKey来知道表是否有主键或外键

    从下面2句SQL语句执行来看, 就知道那一张表有主键PrimaryKey或ForeignKey. 比如,表[Q]和[QQ]既没有主键,也没有外键. 当在SQL语句的条件中,使用“=”,那说明查询出来的 ...

  7. Nginx简单的负载均衡(一)

    Nginx是一个高性能的http和反向代理服务器,官方测试nginx能够支支撑5万并发链接,并且cpu.内存等资源消耗却非常低,运行非常稳定. 1.应用场景 1.http服务器.Nginx是一个htt ...

  8. 解决kbmMW Scheduler在任务中停止任务遇到的问题

    procedure TCustomGridViewCameraDoc.InitSchedule; begin Scheduler.Schedule( procedure(const ASchedule ...

  9. 3.3 Zabbix容器安装

    课程资料:https://github.com/findsec-cn/zabbix 1. yum install docker-latest    :安装最新的docker   ,选择 y  ,等待自 ...

  10. k8s 在Centos上 安装

    k8s安装步骤: 1.所有机器上执行以下命令,准备安装环境:(注意是所有机器,主机master,从机node都要安装) 1.1.安装epel-release源(EPEL (Extra Packages ...