一、题目说明

题目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. 字节、字符、位、bit、byte之间的关系

    字节.字符.位.bit.byte之间的关系 1. 概要 位(bit):是计算机 内部数据 储存的最小单位,表示二进制位,11001100是一个八位二进制数. 电脑记忆体中最小的单位,在二进位电脑系统中 ...

  2. 剑指offer-面试题64-求1+2+...+n-发散思维

    /* 题目: 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C) */ /* 思路: 递归. */ #incl ...

  3. HTML(多行)文本超过部分隐藏,末尾显示(...)

    HTML(多行)文本超过部分隐藏,末尾显示(...) <!DOCTYPE html> <html> <head> <meta charset="ut ...

  4. 【Spring】事务(transactional) - REQUIRES_NEW在JdbcTemplate、Mybatis中的不同表现

    环境 数据库: oracle 11g JAR: org.springframework:spring-jdbc:4.3.8.RELEASE org.mybatis:mybatis:3.4.2 概念 R ...

  5. Mac 终端 Tomcat 环境配置过程

    Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun 和其他一些公司及个人共同开发而成.Tomc ...

  6. Bash脚本编程学习笔记04:测试命令test、状态返回值、位置参数和特殊变量

    我自己接触Linux主要是大学学习的Turbolinux --> 根据<鸟哥的Linux私房菜:基础篇>(第三版) --> 马哥的就业班课程.给我的感觉是这些课程对于bash的 ...

  7. c++多线程编程——初探

    c++多线程并发可以帮助我们挖掘CPU的性能,在我们的思想中,似乎程序都是顺序执行的.这样的结论是建立在:程序是单线程程序.(比如我们平时写的hello world程序). 但是如果程序是多线程的.那 ...

  8. 数据库中间件DBLE学习(一) 基础介绍和快速搭建

    dble基本架构简介 dble是上海爱可生信息技术股份有限公司基于mysql的高可用扩展性的分布式中间件.江湖人送外号MyCat Plus.开源地址 我们首先来看架构图,外部应用通过NIO/AIO进行 ...

  9. Linux系统目录结构和常用目录主要存放内容的说明

    目录结构图 常用目录 /: 根目录 一般根目录下只存放目录,在 linux 下有且只有一个根目录,所有的东西都是从这里开始 当在终端里输入 /home,其实是在告诉电脑,先从 /(根目录)开始,再进入 ...

  10. XMind快捷键汇总

    在 XMind: ZEN 中,快捷键是可以大大提高绘图效率的存在.掌握常用的快捷键组合,就可以在键盘上运指如飞,快速地进行思维导图的绘制.还在等什么?感兴趣的朋友,下面就和小编一起来看看吧! XMin ...