难度:1
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"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
DFS裸搜
注意细节。。
class Solution
{
public:
vector<string>ans;
string tmp;
string s;
int len_s;
string Map[10]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
void dfs(int x)//tmp
{
if(x == len_s)
{
ans.push_back(tmp);
return;
}
//Map[s[x]-'0']
for(int i=0;i<Map[s[x]-'0'].size();i++)
{
tmp+=Map[s[x]-'0'][i];
dfs(x+1);
tmp.pop_back();
}
}
vector<string> letterCombinations(string digits)
{
ans.clear();
tmp="";
s=digits;
len_s=s.size();
dfs(0);
return ans;
}
};