Letter Combinations of a Phone Number
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
解析
2-9分别可以表示各自的字符,输入一个包含2-9字符的字符串,输出所有可能表示的字符串。
自己写的
用一个map来保存数字字符对应的string,建立一个函数用于递归,当输入字符为空时,说明递归了所有的字符长度,关键在于每次递归之后要将当前最后一个字符去掉。
class Solution {
public:
void recursive(string s, vector<string>& v, map<char,string>& digit,string &res){
if(s.size()==0){
cout<<"push: "<<res<<endl;
v.push_back(res);
}
for(int i=0;i<digit[s[0]].size();i++){
res += digit[s[0]][i];
cout<<digit[s[0]]<<" "<<res<<endl;
recursive(s.substr(1), v, digit,res);
res = res.substr(0,res.size()-1);
}
}
vector<string> letterCombinations(string str) {
vector<string> v;
int size = str.size();
if(size==0)
return v;
map<char, string> digit;
digit['2'] = "abc";digit['3'] = "def";
digit['4'] = "ghi";digit['5'] = "jkl";
digit['6'] = "mno";digit['7'] = "pqrs";
digit['8'] = "tuv";digit['9'] = "wxyz";
string res ="";
recursive(str, v, digit,res);
return v;
}
};
递归
使用一个index来表示当前递归的位置,当index=输入字符串长度时结束调用。
class Solution {
public:
void recursive(string s, vector<string>& v, string digit[],string &res,int index){
if(index == s.size()){
cout<<"push: "<<res<<endl;
v.push_back(res);
return;
}
for(int i=0;i<digit[s[index]-'0'].size();i++){
res += digit[s[index]-'0'][i];
recursive(s, v, digit,res,index+1);
res = res.substr(0,res.size()-1);
}
}
vector<string> letterCombinations(string str) {
vector<string> v;
int size = str.size();
if(size==0)
return v;
string digit[] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
string res ="";
recursive(str, v, digit,res,0);
return v;
}
};
迭代
取当前数字代表的字符,然后遍历字符大小,并遍历已有的res字符,将当前单个字符加在后面,遍历一遍之后交换。
下面给出一个参考例子
Explanation with sample input “234”
Initial state:
result = {""}
Stage 1 for number “2”:
result has {""}
candiate is “abc”
generate three strings “” + “a”, “”+“b”, “”+“c” and put into tmp,
tmp = {“a”, “b”,“c”}
swap result and tmp (swap does not take memory copy)
Now result has {“a”, “b”, “c”}
Stage 2 for number “3”:
result has {“a”, “b”, “c”}
candidate is “def”
generate nine strings and put into tmp,
“a” + “d”, “a”+“e”, “a”+“f”,
“b” + “d”, “b”+“e”, “b”+“f”,
“c” + “d”, “c”+“e”, “c”+“f”
so tmp has {“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf” }
swap result and tmp
Now result has {“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf” }
Stage 3 for number “3”:
result has {“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf” }
candidate is “ghi”
generate 27 strings and put into tmp,
add “g” for each of “ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”
add “h” for each of “ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”
add “h” for each of “ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”
so, tmp has
{“adg”, “aeg”, “afg”, “bdg”, “beg”, “bfg”, “cdg”, “ceg”, “cfg”
“adh”, “aeh”, “afh”, “bdh”, “beh”, “bfh”, “cdh”, “ceh”, “cfh”
“adi”, “aei”, “afi”, “bdi”, “bei”, “bfi”, “cdi”, “cei”, “cfi” }
swap result and tmp
Now result has
{“adg”, “aeg”, “afg”, “bdg”, “beg”, “bfg”, “cdg”, “ceg”, “cfg”
“adh”, “aeh”, “afh”, “bdh”, “beh”, “bfh”, “cdh”, “ceh”, “cfh”
“adi”, “aei”, “afi”, “bdi”, “bei”, “bfi”, “cdi”, “cei”, “cfi” }
Finally, return result.
class Solution {
public:
vector<string> letterCombinations(string digits) {
if (digits.empty()) return {};
vector<string> res{""};
string dict[] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
for (int i = 0; i < digits.size(); ++i) {
int num = digits[i]-'0';
if(num < 0 || num > 9) break;
string str = dict[num];
vector<string> tmp;
for (int j = 0; j < str.size(); ++j) {
for(int k=0; k<res.size(); k++){
tmp.push_back(res[k] + str[j]);
}
}
res = tmp;
}
return res;
}
};
参考
http://www.cnblogs.com/grandyang/p/4452220.html
https://leetcode.com/problems/letter-combinations-of-a-phone-number/discuss/8090/Iterative-c%2B%2B-solution-in-0ms