76. 最小覆盖子串

  • 时间:
  • 浏览:
  • 来源:互联网

太菜不会,看的别人的题解

https://leetcode-cn.com/problems/minimum-window-substring/solution/c-zuo-you-zhi-zhen-fa-xiang-jie-by-yizhe-shi/ 

 

    string minWindow(string s, string t) {
        int minlen = 0x3f3f3f3f,curlen = 0,start = 0;
        string ans = "";
        unordered_map<char,int> dict;
        for(int i = 0;i < t.size();i++) dict[t[i]]++;
        int j = 0;
        for(int i = 0;i < s.size();i++)
        {
            dict[s[i]]--;
            if(dict[s[i]] >= 0) 
                curlen++;
            while(curlen == t.size())
            {
                //cout<<j<<" "<<i<<endl;
                if(i - j + 1 < minlen)
                {
                    start = j;
                    minlen = i - j + 1;
                    ans = s.substr(start,minlen);
                }
                dict[s[j]]++;
                if(dict[s[j]] > 0)    
                    curlen--;
                j++;
            }
        }
        return ans;
    }

 

本文链接http://element-ui.cn/news/show-341882.aspx