一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

For example,

S = “ADOBECODEBANC”

T = “ABC”

Minimum window is “BANC”.

Note:

If there is no such window in S that covers all characters in T, return the empty string “”.

If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

(二)解题

题目大意:给定两个字符串S和T,求S中的包含T中所有字符的最小子串。

博主内心os:hard的题为什么每次都这么难做!

下面分享一下解题思路:

准备工作

遇到这种求子串的题目,不难想到要用到两个指针start和end记录子串在S中的开始和结束序号。

另外,本题还需要用到哈希的思想,故使用两个数组needFind[256]和find[256],前者用来记录我们需要找的字母及其出现次数,后来用来记录当前找到的字母及其出现次数。数组大小为256是因为字符ascii值在0~256之间。

那么,接下来,我们需要判断什么时候确定找到了一个子串包含T中的所有字符呢?

这里我们定义一个整形数count来记录“有效字符数”。那又有问题了,什么是有效字符?

假设T中有1个字符A,当我们找到第一个的时候count++,当我们找到第二个的时候超过了我们需要找的,故为无效字符,count不变。

又回到上一个问题,count等于T的长度的时候,就确定找到了一个子串包含T中的所有字符。

算法过程

做好如上的准备之后,就是整个算法的思想:

用[start,end]来标记窗口的起始位置,end一直向后扩张,并且find[]不停的记录已找到的字符及其出现的次数,count一直记录有效字符数。

当count等于字符串T的长度时,表示找到了符合要求的子串,这时,就需要对begin进行处理了。

  • 当S[begin]为不需要找的字符时,需要去除。如S=“CDAB”,T=“AB”,这时CD就需要去除
  • 当S[begin]需要找的字符,但超过了需要找的次数。如S=“AAB”,T=”AB”,这时A就需要去除

这样就找到了当前窗口下的最小子串,然后end继续向后扩张,重复如上步骤。

简而言之就是,扩张收缩在扩张在收缩……

具体的算法请看下面代码:

class Solution {
public:
    string minWindow(string s, string t) {
        int needFind[256] = {0};//记录需要找的字符及其出现次数
        int find[256] = {0};//记录找到的字符及其出现次数
        int count = 0;//记录有效字符数
        int begin=0,end=0;//记录当前窗口的起始位置和结束为止
        int minbegin = 0 , minlen = 2147483647;//记录最小窗口及其大小
        for(int i = 0 ; i < t.size() ; i++) needFind[t[i]]++;//初始化需要找的字符及其需要出现的次数
        for(;end<s.length();end++)
        {
            if(needFind[s[end]]==0) continue;//如果不是需要找的字符就继续下一个
            find[s[end]]++;//找到的需要找的字符,其次数加1
            if(find[s[end]]<=needFind[s[end]]) count++;.//如果找到的字符的次数小于或等于需要找的次数,则记为有效字符
            if(count==t.length())//找到了包含T中所有字符的子串
            {
                while(begin<end) {//去除窗口前面的无效字符
                    if(needFind[s[begin]]==0) begin++;//不需要找的字符去除
                    else if(find[s[begin]] > needFind[s[begin]])//超过次数的字符去除
                    {
                        find[s[begin]]--;//去除后次数减1
                        begin++;
                    }
                    else break;
                }
                int templen = end-begin+1;//计算当前窗口的长度
                if(minlen>templen)
                {
                    minlen = templen;
                    minbegin = begin;
                }
            }
        }
        return minlen == 2147483647 ?"":s.substr(minbegin,minlen);如果没有找到就返回“”,反之返回找到的子串
    }
};

【一天一道LeetCode】#76. Minimum Window Substring的更多相关文章

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

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

  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

    原题地址 用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符 1. 扩展窗口.向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移 2. 收缩窗口.向右调整 ...

  5. 刷题76. Minimum Window Substring

    一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...

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

  7. 【LeetCode】76. Minimum Window Substring 最小覆盖子串(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址: https://leet ...

  8. 76. Minimum Window Substring

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

  9. [Leetcode][JAVA] Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

随机推荐

  1. win10安装配置vs community 2015+opencv3.1.0

    下载并安装Visual Studio Community 2015.具体安装步骤自行解决.下载地址: https://www.visualstudio.com/ 下载opencv3.1.0,并解压.地 ...

  2. python通过token登录,并爬取数据实例

    from bs4 import BeautifulSoup import requests class Zabbix(object): def __init__(self, headers): sel ...

  3. SQL Server AlwaysON从入门到进阶(2)——存储

    本文属于SQL Server AlwaysON从入门到进阶系列文章 前言: 本节讲解关于SQL Server 存储方面的内容,相对于其他小节而言这节比较短.本节会提供一些关于使用群集或者非群集系统过程 ...

  4. Android开发技巧——设置系统状态栏颜色

    开门见山,先来三张效果图: 然后我们再来讲如何实现以及如何快速地实现. 如何实现 实现设置系统状态栏颜色需要至少在Android 4.4.2(API 19)以上.这是因为,在这个版本以下,没有任何的A ...

  5. Android简易实战教程--第四十八话《Android - Timer、TimerTask和Handler实现倒计时》

    之前本专栏文章中的小案例有写到:第三十九话<Chronometer实现倒计时> 以及使用异步实现倒计时:第三十三话< AsyncTask异步倒计时> 本篇文章 结合Timer. ...

  6. 第一个Angular2的样例

    欢迎跟我一起学习Angular2 本文根据angular2官网手动敲码得来: 本文地址:http://blog.csdn.net/sushengmiyan 本文作者:苏生米沿 - 开发环境搭建 - 配 ...

  7. Dynamics CRM2016 Set Values of all Data Types using Web API

    之前的博客里有谈到了web api的增删改查,里面会涉及到各种类型字段的赋值,因为时间和精力关系,没有对所有的字段类型一一测试,这篇博文中给出了全部的 http://inogic.com/blog/2 ...

  8. Android简易实战教程--第三十五话《音乐播放》

    已经好几天不更新博客了,今天轻松一点模拟个简单的"音乐播放器".1分钟看完~ 整个简单布局,加几个控制按钮: <LinearLayout xmlns:android=&quo ...

  9. How to Collect Bne Log Files for GL Integrators

    In this Document   Goal   Solution APPLIES TO: Oracle General Ledger - Version 11.0 and laterInforma ...

  10. 不应滥用named let

    > (define (f x) x) > (define (g x) (let rec((x x)) x)) > (define a '(1 2 3)) > (f a) ( ) ...