[LeetCode]76.Minimum Window Substring,leetcode76.minimum

來源:互聯網
上載者:User

[LeetCode]76.Minimum Window Substring,leetcode76.minimum

題目

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 emtpy string “”.

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

分析

詳細些參考:[演算法系列之二十二]包含T全部元素的最小子視窗

代碼

/*--------------------------------------------*   日期:2015-02-24*   作者:SJF0115*   題目: 76.Minimum Window Substring*   網址:https://oj.leetcode.com/problems/minimum-window-substring/*   結果:AC*   來源:LeetCode*   總結:------------------------------------------------*/#include <iostream>#include <algorithm>#include <climits>using namespace std;class Solution {public:    string minWindow(string S, string T) {        int slen = S.size();        int tlen = T.size();        if(slen <= 0 || tlen <= 0){            return "";        }//if        int minWinStart = 0,minWinEnd = 0;        int minWinLen = INT_MAX;        // 儲存到目前為止遇到過的T中字元總數        int count = 0;        // 儲存T中不同字元的總數        int needFind[256] = {0};        for(int i = 0;i < tlen;++i){            ++needFind[T[i]];        }//for        // 儲存到目前為止遇到過的不同字元的總數        int hasFound[256] = {0};        int val;        for(int start = 0,end = 0;end < slen;++end){            val = S[end];            // 跳過不在T中的字元            if(needFind[val] == 0){                continue;            }//if            ++hasFound[val];            if(hasFound[val] <= needFind[val]){                ++count;            }//if            // 找到一個有效視窗            if(count == tlen){                int startVal = S[start];                while(needFind[startVal] == 0 ||                      hasFound[startVal] > needFind[startVal]){                    if(hasFound[startVal] > needFind[startVal]){                        --hasFound[startVal];                    }//if                    ++start;                    startVal = S[start];                }//while                // 更新最小視窗                int curWinLen = end - start + 1;                if(curWinLen < minWinLen){                    minWinLen = curWinLen;                    minWinStart = start;                    minWinEnd = end;                }//if            }//if        }//for        if(count != tlen){            return "";        }//if        return S.substr(minWinStart,minWinEnd - minWinStart + 1);    }};int main() {    Solution solution;    string S("acbbaca");    string T("aba");    cout<<solution.minWindow(S,T)<<endl;}

已耗用時間

相似題目:

[經典面試題][搜狗]在一個字串中尋找包含全部出現字元的最小字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.