题目:

Given a digit string, 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.

【LeetCode】017. Letter Combinations of a Phone Number-LMLPHP

Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

  

题解:

Solution 1 ()

class Solution {
public:
void dfs(string digits, int level, vector<string>& vv, string& s, vector<string> dict){
if(level >= digits.size()) {
vv.push_back(s);
return;
}
string tmp = dict[digits[level] - ''];
for(int i=; i<tmp.size(); ++i) {
s.push_back(tmp[i]);
dfs(digits, level+, vv, s, dict);
s.pop_back();
}
}
vector<string> letterCombinations(string digits) {
if(digits.empty()) return vector<string>();
vector<string> dict = {"abc","def","ghi","jkl",
"mno","pqrs","tuv","wxyz"};
vector<string> vv;
string s;
dfs(digits, , vv, s, dict);
return vv;
}
};
05-12 03:25