题目描述:
Given a string containing digits from 2-9
inclusive, 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. Note that 1 does not map to any letters.
Example:
Input: "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.
算法
1,枚举
设定结果向量为res(最后的输出值),对数字序列从左往右进行一个循环操作,对于某一个数字位置进行如下操作:
- 依次取出当前结果向量中的各个字符串
- 对取出的每一个字符串分别分次的在其后加上当前数字所对应的字符,并将其添加到结果向量中
- 删除之前结果向量的字符串,只保留前一个步骤添加的所有字符串
算法实现:
class Solution {
public:
vector<string> letterCombinations(string digits) {
map<char,vector<char>> m;
vector<string> res;
m['2'].push_back('a'),m['2'].push_back('b'),m['2'].push_back('c');
m['3'].push_back('d'),m['3'].push_back('e'),m['3'].push_back('f');
m['4'].push_back('g'),m['4'].push_back('h'),m['4'].push_back('i');
m['5'].push_back('j'),m['5'].push_back('k'),m['5'].push_back('l');
m['6'].push_back('m'),m['6'].push_back('n'),m['6'].push_back('o');
m['7'].push_back('p'),m['7'].push_back('q'),m['7'].push_back('r'),m['7'].push_back('s');
m['8'].push_back('t'),m['8'].push_back('u'),m['8'].push_back('v');
m['9'].push_back('w'),m['9'].push_back('x'),m['9'].push_back('y'),m['9'].push_back('z');
for(int i = 0; i < digits.length(); i ++)
{
if(res.empty())
{
string ss;
for(int j = 0; j < m[digits[i]].size(); j ++){
res.push_back(ss += m[digits[i]][j]);
ss = "";
}
}
else{
int res_length_now = res.size();
for(int k = 0; k < res_length_now; k++)
{
string s_suanz;
for(int j = 0; j < m[digits[i]].size(); j ++){
s_suanz = res[k];
res.push_back(s_suanz += m[digits[i]][j]);
}
}
for(int k = 0; k < res_length_now; k++)res.erase(res.begin());
}
}
return res;
}
};
2,回溯法(深度优先搜索)
算法描述:
- 设定一个字符串变量,在遍历数字时不断在后面增加字符,直到数字遍历到最后一位,这时就生成了一个解;
- 在最深位置,数字的最后一位对应的字符都遍历完成后,回退到上一个数字所对应的字符串,换一个该数字对应的字符进行搜索;
- 如此得到所有结果
算法
class Solution {
public:
void dfs(string digits,string now,int index,vector<string> &res,map<char,vector<char>> m){
if(now.length() == digits.length()) {
res.push_back(now);
return ;
}
for(int i = 0; i < m[digits[index]].size(); i ++)
{
string suanz_now = now;
now += m[digits[index]][i];
dfs(digits,now,index+1,res,m);
now = suanz_now;
}
}
vector<string> letterCombinations(string digits) {
map<char,vector<char>> m;
vector<string> res;
m['2'].push_back('a'),m['2'].push_back('b'),m['2'].push_back('c');
m['3'].push_back('d'),m['3'].push_back('e'),m['3'].push_back('f');
m['4'].push_back('g'),m['4'].push_back('h'),m['4'].push_back('i');
m['5'].push_back('j'),m['5'].push_back('k'),m['5'].push_back('l');
m['6'].push_back('m'),m['6'].push_back('n'),m['6'].push_back('o');
m['7'].push_back('p'),m['7'].push_back('q'),m['7'].push_back('r'),m['7'].push_back('s');
m['8'].push_back('t'),m['8'].push_back('u'),m['8'].push_back('v');
m['9'].push_back('w'),m['9'].push_back('x'),m['9'].push_back('y'),m['9'].push_back('z');
if(digits.length() == 0) return res;
dfs(digits,"",0,res,m);
return res;
}
};