0
点赞
收藏
分享

微信扫一扫

Android架构组件:MVVM模式的实战应用于数据绑定技巧

witmy 2024-09-05 阅读 4

 

class Solution {
public:
    string minWindow(string s, string t) 
    {
        int hash1[128] = { 0 };//统计 t 中所有字母出现次数
        int kinds = 0;//统计 t 中字母种类
        int minlen = INT_MAX;
        int begin = -1;
        for(auto ch : t)
        {
            if(hash1[ch]++ == 0)
            {
                kinds++;
            }
        }

        int hash2[128] = { 0 };//统计窗口内每个字母出现频次
        for(int left = 0,right = 0,count = 0;right < s.size();right++)
        {
            char in = s[right];
            //进窗口 + 维护
            if(++hash2[in] == hash1[in])
            {
                count++;
            }
            //判断出窗口
            while(count == kinds)
            {
                if(minlen > right - left + 1)
                {
                    minlen = right - left + 1;
                    begin = left;
                }
                //出窗口 + 维护 count
                char out = s[left++];
                if(hash2[out]-- == hash1[out])
                {
                    count--;
                }
            }
        }
        if(begin == -1)
        {
            return "";
        }
        else
        {
            return s.substr(begin,minlen);
        }
    }
};

 

举报

相关推荐

0 条评论