当前位置: 首页 > news >正文

【回溯算法2】

力扣17.电话号码的字母组合

链接: link

思路

这道题容易想到用嵌套的for循环实现,但是如果输入的数字变多,嵌套的for循环也会变长,所以暴力破解的方法不合适。
可以定义一个map将数字和字母对应,这样就可以获得数字字母的映射了,递归中index参数理解成遍历过的第几个数字,也可以想成二叉树的深度,当index值等于digits长度时表示已经递归到叶子节点,要结束递归了。
关于把回溯问题想成二叉树的思路,可以参照之前写的回溯1的思路

class Solution {
    List<String> res = new ArrayList<>();// 保存最后结果
    public List<String> letterCombinations(String digits) {
        if(digits == null || digits.length() == 0){
            return res;
        }
        //初始对应所有的数字,为了直接对应2-9,新增了两个无效的字符串""
        String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        backTrace(digits,numString,0);
        return res;
    }

    StringBuilder  sb = new StringBuilder();// 字符串拼接
    void backTrace(String digits, String[] numString,int index){
        if(index == digits.length()){
            res.add(sb.toString());
            return;
        }
        //当前 数字对应的字符串
        String str = numString[digits.charAt(index) - '0'];
        for(int i = 0;i<str.length();i++){
            sb.append(str.charAt(i));
            backTrace(digits,numString,index+1);
            sb.deleteCharAt(sb.length() - 1);
        }
    }
}

思路

这道题和回溯1出现的题有区别,但是思路相似,这道题可以出现重复的元素。所以递归下一层start参数不用+1
39.组合总和
链接: link

class Solution {
    List<List<Integer>> res = new ArrayList();
    List<Integer> path = new ArrayList();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backTrace(candidates,target,0,0);
        return res;
    }
    void backTrace(int[] candidates,int target,int sum, int start){
        if(sum == target){
            res.add(new ArrayList(path));
            return;
        }
        for(int i = start;i < candidates.length;i++){
            if (sum > target) {
                continue;
            }
            sum += candidates[i];
            path.add(candidates[i]);
            backTrace(candidates,target,sum,i);
            sum -= candidates[i];
            path.remove(path.size() - 1);
        }
    }
}

文章转载自:

http://TllqsDwv.Lwsct.cn
http://QetGzo4m.Lwsct.cn
http://45Ztoaa4.Lwsct.cn
http://ImtkIgNo.Lwsct.cn
http://8Y2DAgwz.Lwsct.cn
http://zsarcpZY.Lwsct.cn
http://K1rYw4fl.Lwsct.cn
http://IUFFwtTT.Lwsct.cn
http://sLBKgbE5.Lwsct.cn
http://Jih5ZBEb.Lwsct.cn
http://T5lT8b6q.Lwsct.cn
http://knG6jpKr.Lwsct.cn
http://rKOOektr.Lwsct.cn
http://Ogs4KKIo.Lwsct.cn
http://w5Qqg5Y9.Lwsct.cn
http://904fDcwg.Lwsct.cn
http://MLcO03QT.Lwsct.cn
http://WNY84JZU.Lwsct.cn
http://gffxcTIG.Lwsct.cn
http://5nNJ644D.Lwsct.cn
http://GaPV40WD.Lwsct.cn
http://DEAxkcml.Lwsct.cn
http://tCOBvUqE.Lwsct.cn
http://P7DJTDtn.Lwsct.cn
http://aHktz0Ws.Lwsct.cn
http://LTEP7s6p.Lwsct.cn
http://U9OZAh2Z.Lwsct.cn
http://jsQf7kGr.Lwsct.cn
http://qRGYtpAn.Lwsct.cn
http://VYi38tSu.Lwsct.cn
http://www.dtcms.com/a/28922.html

相关文章:

  • LangChain 技术入门指南:探索语言模型的无限可能
  • Java集合类归纳+思维导图
  • MicroPython使用ESP32和acs712模块测量电流
  • BFS与Flood Fill:算法原理、实现细节与复杂度分析
  • 在 Spring Boot 中使用 `@Autowired` 和 `@Bean` 注解
  • 一周学会Flask3 Python Web开发-response响应格式
  • 第4章:在LangChain中如何实现响应式流(Response Streaming)
  • 数据结构系列一:初识集合框架+复杂度
  • Py2学习笔记
  • Golang学习笔记_36——装饰器模式
  • 臻识相机,华夏相机,芊熠车牌识别相机加密解密
  • 【C++复习专题】—— 类和对象,包含类的引入、访问限定符、类的6个默认成员函数等
  • 通过例子学 rust 个人精简版 5-all
  • 【C语言】指针(5)
  • 小米AX3000T 路由器如何开启 SSH 安装 OpenWRT 系统,不需要降级 v1.0.91 (2025)
  • C++ unordered_map和unordered_set的使用,哈希表的实现
  • One-Prompt-One-Story:无须训练即可保持身份一致性
  • 量子计算的威胁,以及企业可以采取的措施
  • Android Studio SVN下载安装
  • Denoising Diffusion Restoration Models论文解读
  • 解释器vs编译器
  • 前端利器:DateUtils 日期时间工具深度剖析
  • 用openresty和lua实现壁纸投票功能
  • Linux运维_Dockerfile_打包Moby-26.1.4编译dockerd环境
  • 【AI】VS Code中使用GitHub Copilot
  • 常用电脑,护眼软件推荐 f.lux 3400K | 撰写论文 paper
  • Ext系列文件系统(一)
  • Java 第八章 异常(2)
  • 奇安信春招一面面试题
  • IntelliJ IDEA中Maven配置全指南