leetcode算法每天一题 017: 电话号码的字母组合:回溯算法(字符串的一些操作)dfs

本文介绍了如何使用C++实现电话号码对应的字母组合。通过回溯算法,将数字映射到特定的字母组合,如'2'对应'abc'等。在不同版本的代码中,逐步修正了逻辑错误,最终得到正确输出所有可能的字母组合。该问题涉及到深度优先搜索(DFS)的应用,以及字符串操作和递归的知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

在这里插入图片描述

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 ) //取消影响
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值