leetcode17题解
题目描述
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
思路
大模拟,递归查找即可
代码
class Solution {
public:
void calc(const string trans[9],const string&digits ,vector<string>& res,
const int max_size,string curr="",int depth=0)
{
if(depth==max_size) res.push_back(curr);
else
{
for(const auto &c:trans[digits[depth]-'1']){
curr.push_back(c);
calc(trans,digits,res,max_size,curr,depth+1);
curr.pop_back();
}
}
}
vector<string> letterCombinations(string digits) {
string trans[9]={
"",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};
vector<string> res;
calc(trans,digits,res,digits.size());
return res;
}
};