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.
Input:Digit string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].class Solution {
public:
vector<string> letterCombinations(string digits) {
//digits传递的是数字比如"23"
vector<string> res;
//这一步好难 如果为空,则返回空
if(digits=="") return {};
string tmp(digits.size(),' ');
dfs(res,tmp,digits,0);
return res;
}
//pos 表示添加的第几个!
void dfs(vector<string> &res,string &tmp,string digits,int pos)
{
string table[]={" ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
//找不到问题出在哪里
if(pos==digits.size())
{
res.push_back(tmp);
return;
}
for(int i=0;i<table[digits[pos]-'0'].size();i++)
{
tmp[pos]=table[digits[pos]-'0'][i];
dfs(res,tmp,digits,pos+1);
}
}
};
1: 组合问题。
2 pos传递的是组合的位置
3 当输入的是空集怎么处理
4 tmp[0],tmp[1]实现了覆盖。不需要退出选的值