LeetCode (17)Letter Combinations of a Phone Number

(17)Letter Combinations of a Phone Number

題目:通過所給字符串,返回按下數字可能返回的字符串組合,數字和字符的關係如同手機鍵盤一樣。

例子:

輸入:字符串爲“23”。
輸出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]。

首先想到的就是隊列,不停地通過隊列的每一組字符串相加獲得下一組字符串,然後再把下一組字符串壓入隊列中,這樣能夠實現整個字符串的所有可能性的羅列。

當然在C++中vector有着類似隊列和棧的功能,每一次拿出v.back()做基礎字符串,然後v.pop_back(),直到v.empty(),將做好的新字符串放到v_temp中,最後v=v_temp,慢慢將所有的字符串處理完成就可以了。

下面是代碼:

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        int len_str = digits.size();
        char c;
        string word[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        int i,j;
        vector<string> s;
        int len = 0;
        for(i = 0; i < len_str; i ++){
            c = digits[i];
            if(c == '1'){
            }
            else if(c == '0'){
                vector<string> temp_v;
                if(len == 0){
                    temp_v.push_back(" ");
                    s = temp_v;
                    len ++;
                }
                else{
                    while(!s.empty()){
                        string str_temp = "";
                        str_temp += s.back();
                        s.pop_back();
                        str_temp += " ";
                        temp_v.push_back(str_temp);
                    }
                    s = temp_v;
                }
            }
            else if(c == '2'||c == '3'||c == '4'||c == '5'||c == '6'||c == '8'){
                vector<string> temp_v;
                if(len == 0){
                    string str_temp = "";
                    string s1 = str_temp;
                    s1.append(word[c - '0'].substr(0,1));
                    temp_v.push_back(s1);
                    string s2 = str_temp;
                    s2.append(word[c - '0'].substr(1,1));
                    temp_v.push_back(s2);
                    string s3 = str_temp;
                    s3.append(word[c - '0'].substr(2,1));
                    temp_v.push_back(s3);
                    s = temp_v;
                    len ++;
                }
                else{
                    while(!s.empty()){
                        string str_temp = "";
                        str_temp += s.back();
                        s.pop_back();
                        string s1 = str_temp;
                        s1.append(word[c - '0'].substr(0,1));
                        temp_v.push_back(s1);
                        string s2 = str_temp;
                        s2.append(word[c - '0'].substr(1,1));
                        temp_v.push_back(s2);
                        string s3 = str_temp;
                        s3.append(word[c - '0'].substr(2,1));
                        temp_v.push_back(s3);
                    }
                    s = temp_v;
                }
            }
            else if(c == '7'||c == '9'){
                vector<string> temp_v;
                if(len == 0){
                    string str_temp = "";
                    string s1 = str_temp;
                    s1.append(word[c - '0'].substr(0,1));
                    temp_v.push_back(s1);
                    string s2 = str_temp;
                    s2.append(word[c - '0'].substr(1,1));
                    temp_v.push_back(s2);
                    string s3 = str_temp;
                    s3.append(word[c - '0'].substr(2,1));
                    temp_v.push_back(s3);
                    string s4 = str_temp;
                    s4.append(word[c - '0'].substr(3,1));
                    temp_v.push_back(s4);
                    s = temp_v;
                    len ++;
                }
                else{
                    while(!s.empty()){
                        string str_temp = "";
                        str_temp += s.back();
                        s.pop_back();
                        string s1 = str_temp;
                        s1.append(word[c - '0'].substr(0,1));
                        temp_v.push_back(s1);
                        string s2 = str_temp;
                        s2.append(word[c - '0'].substr(1,1));
                        temp_v.push_back(s2);
                        string s3 = str_temp;
                        s3.append(word[c - '0'].substr(2,1));
                        temp_v.push_back(s3);
                        string s4 = str_temp;
                        s4.append(word[c - '0'].substr(3,1));
                        temp_v.push_back(s4);
                    }
                    s = temp_v;
                }
            }
        }
        return s;
    }
};
發佈了48 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章