一. 题目描述
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"].
二. 题目分析
题目的大意是,在手机上按字母,给出所按的数字键,问所有的按的字母的情况。该题使用DFS即可完成,难度不大。
三. 示例代码
class Solution {
public:
vector<string> result;
vector<string> letterCombinations(string digits) {
if (digits == "") return result;
static string k[8] = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
string temp = "";
dfs(digits, temp, k);
return result;
}
private:
void dfs(string dig, string temp, string str[])
{
// find函数用于寻找某个序列的在string中第一次出现的位置,防止重复
if (dig.size() == 0 && find(result.begin(), result.end(), temp) == result.end())
{
result.push_back(temp);
return;
}
else
{
for (int i = 0; i < str[dig[0] - '2'].size(); ++i)
{
temp += str[dig[0] - '2'][i]; // 在最后添加一个字符
dfs(dig.substr(1, dig.size()), temp, str);
temp = temp.substr(0, temp.size() - 1); // 去掉最后一个字符
}
return;
}
}
};