算法奇妙屋(二十九)-递归、回溯与剪枝的综合问题 2

文章目录

一. 力扣 77. 组合

1. 题目解析

组合是对顺序不敏感的, 即(1,2)和(2,1)相等

2. 算法原理

3. 代码

java 复制代码
class Solution {
    List<List<Integer>> ret;
    List<Integer> path;
    int n;
    int k;
    public List<List<Integer>> combine(int _n, int _k) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        n = _n;
        k = _k;
        dfs(1);
        return ret;
    }
    void dfs(int pos) {
        if (k == path.size()) {
            ret.add(new ArrayList(path));
            return;
        }
        for (int i = pos; i <= n; i++) {
            // 记录路径
            path.add(i);
            // 递归下一层
            dfs(i + 1);
            // 回溯
            path.remove(path.size() - 1);
        }
    }
}

二. 力扣 494. 目标和

1. 题目解析

这道题相信大家应该很眼熟, 在算法奇妙屋(二十二)-01背包问题(动态规划)这篇文章中讲过动态规划的解法

2. 解法一原理

3. 解法二原理

4. 算法一代码

java 复制代码
class Solution {
    int ret, cur;
    double a;
    public int findTargetSumWays(int[] nums, int target) {
        int sum = ret = cur = 0;
        for (int x : nums) {
            sum += x;
        }
        a = (sum + target) / 2.0;
        dfs(nums, 0);
        return ret;
    }
    void dfs(int[] nums, int pos) {
        if (cur == a) {
            ret++;
        }
        for (int i = pos; i < nums.length; i++) {
            cur += nums[i];
            dfs(nums, i + 1);
            cur -= nums[i];
        }
    }
}

5. 算法二代码

java 复制代码
class Solution {
    int ret;
    int path;
    int aim;

    public int findTargetSumWays(int[] nums, int target) {
        ret = path = 0;
        aim = target;
        dfs(nums, 0, 0);
        return ret;
    }

    void dfs(int[] nums, int pos, int path) {
        if (pos == nums.length) {
            if (path == aim) {
                ret++;
            }
            return;
        }
        dfs(nums, pos + 1, path + nums[pos]);
        dfs(nums, pos + 1, path - nums[pos]);
    }
}

三. 力扣 39. 组合总和

1. 题目解析

这道题让求的是组合+求和 = 目标值, 其中组合中元素可以重复

2. 算法原理

3. 代码

java 复制代码
class Solution {
    List<List<Integer>> ret;
    List<Integer> path;
    int t;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        t = target;
        dfs(candidates,0, 0);
        return ret;
    }

    void dfs(int[] nums, int sum, int pos) {
        if (sum >= t) {
            if (sum == t) {
                ret.add(new ArrayList(path));
            }
            return;
        }
        for (int i = pos; i < nums.length; i++) {
            if (sum + nums[i] <= t) {
                path.add(nums[i]);
                dfs(nums, sum + nums[i], i);
                path.remove(path.size() - 1);
            }
        }
    }
}

四. 力扣 784. 字母大小写全排列

1. 题目解析

按原来顺序, 英文字符变为大写和小写来进行全排列

2. 算法原理

3. 代码

java 复制代码
class Solution {
    StringBuilder path;
    List<String> ret;
    public List<String> letterCasePermutation(String s) {
        ret = new ArrayList<>();
        path = new StringBuilder();
        String ss = s.toLowerCase();
        dfs(ss, 0);
        return ret;
    }

    void dfs(String s, int pos) {
        if (pos == s.length()) {
            ret.add(path.toString());
            return;
        }
        char ch = s.charAt(pos);
        path.append(ch);
        dfs(s, pos + 1);
        path.deleteCharAt(path.length() - 1);
        if (ch < '0' || ch > '9') {
            path.append((char) (ch - 32));
            dfs(s, pos + 1);
            path.deleteCharAt(path.length() - 1);
        }
    }
}
相关推荐
迈巴赫车主几秒前
蓝桥杯20560逃离高塔
java·开发语言·数据结构·算法·职场和发展·蓝桥杯
泯仲8 分钟前
Ragent项目7种设计模式深度解析:从源码看设计模式落地实践
java·算法·设计模式·agent
dulu~dulu13 分钟前
算法---寻找和为K的子数组
笔记·python·算法·leetcode
moonsea020323 分钟前
【无标题】
算法
佑白雪乐44 分钟前
<ACM进度212题>[2026-3-1,2026-3-26]
算法·leetcode
穿条秋裤到处跑1 小时前
每日一道leetcode(2026.03.26):等和矩阵分割 II
算法·leetcode·矩阵
平凡灵感码头1 小时前
C语言 printf 数据打印格式速查表
c语言·开发语言·算法
哔哔龙1 小时前
Android OpenCV 实战:图片轮廓提取与重叠轮廓合并处理
android·算法
hz_zhangrl1 小时前
CCF-GESP 等级考试 2026年3月认证C++三级真题解析
c++·算法·程序设计·gesp·gesp2026年3月·gesp c++三级
x_xbx1 小时前
LeetCode:1. 两数之和
数据结构·算法·leetcode