Leetcode刷题76(最小覆盖子串)困难

题目描述:

给你一个字符串 S、一个字符串 T,请在字符串 S 里面找出:包含 T 所有字母的最小子串。

示例:

输入: S = "ADOBECODEBANC", T = "ABC"
输出: "BANC"
说明:

如果 S 中不存这样的子串,则返回空字符串 ""。
如果 S 中存在这样的子串,我们保证它是唯一的答案。

思路:

滑动窗口思想,使用左右两个指针。

通过答案:

class Solution {
    public String minWindow(String s, String t) {
        if(s.length()==0||t.length()==0){       //若有一个串为空,则返回“”
            return "";
        }
        int[] map=new int[128];      //新建数组

        for(int i=0;i=0){     //当数组中次数大于0时,表示当前一个字符包含
                count--;
            } 
            //当滑动窗口包含t中所有字母时,开始移动左指针,缩小窗口
            while(count==0){
                int temp_len=right-left+1;    //当前窗口长度
                if(temp_len0){    //当此次数大于0时,表示窗口不包含t中某一字母
                    count++;
                }
                left++;   //左指针右移
            }
            right++;   //右指针左移
        }
        return s.substring(ans_left,ans_right+1);   //返回最小子串
    }
}

 

你可能感兴趣的:(LeetCode刷题)