一、题目说明

题目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. 【ffmpeg 视频下载】使用cmd视频下载

    概述 ffmpeg是什么? FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.并且,很多视频播放器都是采用他的内核. 安装与使用 安装ffmpeg ffmpeg下载 ...

  2. 利用js+ajax在jsp与servlet间进行简单数据交换

    直接上代码 jsp <%@ page language="java" contentType="text/html; charset=utf-8" pag ...

  3. linux下安装lxml包

    爬虫项目需要用到lxml包,解析html文件,但是linux服务器没有lxml包, 服务器中python版本是3.8. 直接使用命令安装: pip install lxml 中途会报错,错误提示我没有 ...

  4. 剑指offer-面试题62-圆圈中最后剩下的数字-约瑟夫环-解法2

    /* 题目: 约瑟夫环问题. 思路: 数学规律 f(n)=0(n=1),[f(n-1,m)+m]%n(n>1) */ #include<iostream> #include<l ...

  5. 【python基础语法】第7天作业练习题

    import keyword ''' # 第一题:简单题 1.什么是全局变量? 2.什么是局部变量? 3.函数内部如何修改全局变量(如何声明全局变量 )? 4.写出已经学过的所有python关键字,分 ...

  6. opencv —— split、merge 通道的分离与合并

    对于三通道或四通道图像,有时要对某一通道的像素值进行修改或展示,这就需要进行通道分离操作.修改后,若要进行结果展示,就需要重新将各通道合并. 通道分离:split 函数 void split (Inp ...

  7. adworld easy_RSA | RSA算法

    题目描述: 解答出来了上一个题目的你现在可是春风得意,你们走向了下一个题目所处的地方 你一看这个题目傻眼了,这明明是一个数学题啊!!!可是你的数学并不好.扭头看向小鱼,小鱼哈哈一笑 ,让你在学校里面不 ...

  8. spring中JdbcTemplate使用

    1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  9. 【笔记】机器学习 - 李宏毅 - 7 - Deep Learning

    深度学习发展历史: 感知机和逻辑回归很像,只是没有\(sigmoid\)激活函数. 深度学习训练的三个步骤: Step1:神经网络(Neural network) Step2:模型评估(Goodnes ...

  10. IDEA如何恢复到以前的代码

    虽然说IDEA的Ctrl+z撤销操作和Ctrl+Shift+Z重做操作为书写代码提供了很大的便利 但是在各种原因不小心失误撤销后又想不起来代码是怎么写的时候就非常尴尬,这时候就需要代码恢复到以前的状态 ...