LeetCode (17)Letter Combinations of a Phone Number

来源:互联网 发布:程序员联合网 编辑:程序博客网 时间:2024/06/05 11:29

(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;    }};
0 0