LeetCode || Word Break II

來源:互聯網
上載者:User

標籤:leetcode

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].

A solution is ["cats and dog", "cat sand dog"].

即不僅要確定字串是否能被字典分割,還要找出所有可能的組合。參考word break那題的DP思路,首先,從尾部開始逆向看字串 s ,迴圈截取一個存在的詞(milestone 1),然後在截取的位置遞迴,繼續向前看,繼續截取。。。知道到達頭部,此時組合出一種答案;然後進入milestone 1 處的下一次迴圈,如的milestone 1 ,截取另外一個詞,找另外一個答案。。。




代碼如下:


class Solution {    vector<string> midres;    vector<string> res;    vector<bool> *dp;public:    vector<string> wordBreak(string s, unordered_set<string> &dict) {        int len = s.length();                dp = new vector<bool>[len];        for(int i=0; i<len; ++i){            for(int j=i; j<len; ++j){                if(dict.find(s.substr(i, j-i+1))!=dict.end()){                    dp[i].push_back(true);      //第二維的下標實際是:單詞長度-1                }else{                    dp[i].push_back(false);     //數組第二維用vector,size不一定是n,這樣比n*n節省空間的                }            }        }        func(s, len-1);        return res;    }        void func(const string &s, int i){        if(i>=0){            for(int j=0; j<=i; ++j){                                if(dp[j][i-j]){ //注意此處的第二個下標是 i-j,不是i,因為數組的第二維長度是不固定的,第二維的下標實際是單詞長度-1                                    midres.push_back(s.substr(j, i-j+1));                    func(s, j-1);                    midres.pop_back();  //繼續考慮for迴圈的下一個分段處                }            }            return;        }        else{            string str;            for(int k=midres.size()-1; k>=0; --k){  //注意遍曆的順序是倒序的                str += midres[k];   //注意此處是k,不是i                if(k>0)                    str += " ";            }            res.push_back(str);            return;        }    }};


注意遞迴函式的技巧,用全域變數res來儲存答案,每次遞迴成功到達頭部時將此中間結果儲存到res。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.