一天一道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. Node.js 进程

    process 是全局对象,能够在任意位置访问,是 EventEmitter 的实例. 退出状态码 当没有新的异步的操作等待处理时,Node 正常情况下退出时会返回状态码 0 .下面的状态码表示其他状 ...

  2. JS基础速成(一)

    .t1 { background-color: #ff8080; width: 1100px; height: 40px } 一.JS的变量 1.变量的声明 var num=1;//使用var生命的变 ...

  3. webpack dev server 和 sublime text 配合时需要注意的地方

    参考:https://webpack.js.org/guides/development/ Adjusting Your Text Editor Some text editors have a &q ...

  4. Spinner控件详解

    Spinner控件详解 效果图 修改Spinner样式 在介绍之前,先看一下系统原生的样式 6.x & 5.x系统样式 4.x系统样式 官方文档 XML属性 方法 描述 android:dro ...

  5. Android适配难题全面总结

    支持多种屏幕 Android 可在各种具有不同屏幕尺寸和密度的设备上运行.对于 应用,Android 系统在不同设备中提供一致的开发环境, 可以处理大多数工作,将每个应用的用户界面调整为适应其显示的 ...

  6. Lua热更新(hotfix)

    Lua热更新(hotfix)(金庆的专栏)hotfixLua 5.2/5.3 hotfix. Hot update functions and keep old data.https://github ...

  7. SRAM/DRAM,PROM/EPROM/EEPROM,NOR/NAND FLASH区别

                          SRAM/DRAM,PROM/EPROM/EEPROM,NOR/NAND FLASH区别 RAM / ROM 存储器 ROM和RAM指的都是半导体存储器,R ...

  8. hive 压缩全解读(hive表存储格式以及外部表直接加载压缩格式数据);HADOOP存储数据压缩方案对比(LZO,gz,ORC)

    数据做压缩和解压缩会增加CPU的开销,但可以最大程度的减少文件所需的磁盘空间和网络I/O的开销,所以最好对那些I/O密集型的作业使用数据压缩,cpu密集型,使用压缩反而会降低性能. 而hive中间结果 ...

  9. linuxsvn源代码版本库建立

    linuxsvn源代码版本库建立 下面就要建立代码的版本库做描述: 1.     安装svn版本服务器端 yum install subversion 从镜像下载安装svn服务器端,我们服务器已经安装 ...

  10. How to generate the complex data regularly to Ministry of Transport of P.R.C by DB Query Analyzer

    How to generate the complex data regularly to Ministry of Transport of P.R.C by DB Query Analyzer 1 ...