题目描述
class Solution {
public:
vector<string> letterCombinations(string digits) {
}
};
- 以按键2和3为例子
输入:digits = "23"
输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]
回溯算法
f(){
if(终止条件){回溯终止,不在此代码块调用f() }
执行操作
f()
撤销操作
}
解
v1
输入
"23"
输出
["a","b","c","a","b","c"]
预期结果
["ad","ae","af","bd","be","bf","cd","ce","cf"]
class Solution {
public:
vector<string> result;
vector<string> letterCombinations(string digits) {
string table[] = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
int current_num;
for(int i=0;i<digits.length();i++){//树的宽度是集合的数目 for循环
string res = "";
dfs(res,0,digits,table);
}
return result;
}
void dfs(string res,int depth,string digits,string table[]){
if(depth == digits.length()-1){
result.push_back(res);
}
int current_num = digits[depth]- '0';
current_num = current_num -2;
for(int i=0;i<table[current_num].length();i++){
res.push_back(table[current_num][i]);
dfs(res,depth+1,digits,table);
res.pop_back();//https://www.cnblogs.com/yavn/p/15230678.html
}
}
};
v2
输入
“23”
输出
[“ad”,“ae”,“af”,“bd”,“be”,“bf”,“cd”,“ce”,“cf”,“ad”,“ae”,“af”,“bd”,“be”,“bf”,“cd”,“ce”,“cf”]
预期结果
[“ad”,“ae”,“af”,“bd”,“be”,“bf”,“cd”,“ce”,“cf”]
class Solution {
public:
vector<string> result;
vector<string> letterCombinations(string digits) {
string table[] = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
int current_num;
for(int i=0;i<digits.length();i++){//树的宽度是集合的数目 for循环
string res = "";
dfs(res,0,digits,table);
}
return result;
}
void dfs(string res,int depth,string digits,string table[]){
if(depth == digits.length()){
result.push_back(res);
}else{
int current_num = digits[depth]- '0';
current_num = current_num -2;
for(int i=0;i<table[current_num].length();i++){
res.push_back(table[current_num][i]);
dfs(res,depth+1,digits,table);
res.pop_back();//https://www.cnblogs.com/yavn/p/15230678.html
}
}
}
};
V3
class Solution {
public:
vector<string> result;
vector<string> letterCombinations(string digits) {
string table[] = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
int current_num;
//for(int i=0;i<digits.length();i++){//树的宽度是集合的数目 for循环
string res = "";
dfs(res,0,digits,table);
//}
return result;
}
void dfs(string res,int depth,string digits,string table[]){
if(depth == digits.length()){
result.push_back(res);
}else{
int current_num = digits[depth]- '0';
current_num = current_num -2;
for(int i=0;i<table[current_num].length();i++){
res.push_back(table[current_num][i]);
dfs(res,depth+1,digits,table);
res.pop_back();//https://www.cnblogs.com/yavn/p/15230678.html
}
}
}
};
成功版本
class Solution {
public:
vector<string> result;
vector<string> letterCombinations(string digits) {
string table[] = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
int current_num;
if(digits == "") return {};
string res = "";
dfs(res,0,digits,table);
return result;
}
void dfs(string res,int depth,string digits,string table[]){
if(depth == digits.length()){
result.push_back(res);
}else{
int current_num = digits[depth]- '0';
current_num = current_num -2;
for(int i=0;i<table[current_num].length();i++){
res.push_back(table[current_num][i]);
dfs(res,depth+1,digits,table);
res.pop_back();//C++删除string最后一个字符的几种方法 https://www.cnblogs.com/yavn/p/15230678.html
}
}
}
};
总结
- 回溯算法很像dfs,或者dfs就是。第一步所要做的就是能一直迭代到最深处的叶子节点。
CG
/**
* @param {string} digits
* @return {string[]}
*/
var letterCombinations = function(digits) {
let table = ['abc','def','ghi','jkl','mno','pqrs','tuv','wxyz'];
let res = [];
if(digits.length == 0){return [];}
f(0,'',table,digits,res); // 0为当前回溯的数字,''为结果需要放入res中,后边三个为程序的参数
return res;
};
var f = function(idx,cur,table,digits,res){// https://www.bilibili.com/video/BV1wm4y1S7X2/?
// 终止条件
if(idx == digits.length){
res.push(cur);
return;
}
let str = table[Number(digits[idx])-2] //获取string
for(let char of str){
cur = cur.concat(char);
f(idx+1,cur,table,digits,res);
cur = cur.slice(0,cur.length-1 ) //取消影响
}
}