力扣数组练习题(组合总和、组合总和||)

99 阅读3分钟

组合总和

来源:力扣(LeetCode) 链接:leetcode.cn/problems/co…

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

示例 1:

输入:candidates = [2,3,6,7], target = 7

输出:[[2,2,3],[7]]

解释:

2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。

7 也是一个候选, 7 = 7 。

仅有这两种组合。

示例 2:

输入: candidates = [2,3,5], target = 8

输出: [[2,2,2,2],[2,3,3],[3,5]]

示例 3:

输入: candidates = [2], target = 1

输出: []

提示:

  • 1 <= candidates.length <= 30
  • 2 <= candidates[i] <= 40
  • candidates 的所有元素 互不相同
  • 1 <= target <= 40

代码

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        if (candidates == null || candidates.length == 0) return res;
        Arrays.sort(candidates); // 对数组进行排序
        dfs(candidates, target, 0, new ArrayList<>(), res);
        return res;
    }

    private void dfs(int[] candidates, int target, int index, List<Integer> path, List<List<Integer>> res) {
        if (target == 0) { // 找到一组解
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = index; i < candidates.length; i++) {
            int cur = candidates[i];
            if (target < cur) break; // 剪枝:如果target比当前元素小,直接跳出循环
            path.add(cur);
            dfs(candidates, target - cur, i, path, res); // 递归搜索
            path.remove(path.size() - 1); // 回溯
        }
    }
}

思路分析

  1. 首先对数组进行排序,这是为了优化后面的搜索过程,避免出现重复的解。
  2. 然后从数组的第一个元素开始,对每个元素进行搜索,如果当前元素大于target,那么就直接跳过。
  3. 如果当前元素等于target,那么就找到一组解,将其添加到结果集中。
  4. 如果当前元素小于target,那么就将该元素添加到路径中,然后递归搜索下一个元素。
  5. 如果下一个元素不满足条件,就回溯到上一个元素,继续搜索下一个元素。

这个思路可以用一个dfs函数来实现,其中参数candidates表示数组,target表示目标值,index表示从哪个位置开始搜索,path表示当前搜索的路径,res表示结果集。

在dfs函数中,先判断target是否为0,如果是0,就找到一组解,将其添加到结果集中,然后返回;如果target小于当前元素,那么就剪枝,跳出循环;否则,将当前元素添加到路径中,继续搜索下一个元素,最后回溯。

组合总和||

来源:力扣(LeetCode) 链接:leetcode.cn/problems/co…

给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用 一次 。

注意:解集不能包含重复的组合。

示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8,

输出:

[[1,1,6],[1,2,5],[1,7],[2,6]]

示例 2:

输入: candidates = [2,5,2,1,2], target = 5,

输出:

[[1,2,2],[5]]

提示:

  • 1 <= candidates.length <= 100
  • 1 <= candidates[i] <= 50
  • 1 <= target <= 30

代码

class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        if (candidates == null || candidates.length == 0) return res;
        Arrays.sort(candidates); // 对数组进行排序
        dfs(candidates, target, 0, new ArrayList<>(), res);
        return res;
    }

    private void dfs(int[] candidates, int target, int index, List<Integer> path, List<List<Integer>> res) {
        if (target == 0) { // 找到一组解
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = index; i < candidates.length; i++) {
            if (i > index && candidates[i] == candidates[i - 1]) continue; // 去重
            int cur = candidates[i];
            if (target < cur) break; // 剪枝:如果target比当前元素小,直接跳出循环
            path.add(cur);
            dfs(candidates, target - cur, i + 1, path, res); // 递归搜索下一个元素,从i+1开始
            path.remove(path.size() - 1); // 回溯
        }
    }
}

思路分析

  1. 首先对数组进行排序,这是为了优化后面的搜索过程,避免出现重复的解。
  2. 然后从数组的第一个元素开始,对每个元素进行搜索,如果当前元素大于target,那么就直接跳过。
  3. 如果当前元素等于target,那么就找到一组解,将其添加到结果集中。
  4. 如果当前元素小于target,那么就将该元素添加到路径中,然后递归搜索下一个元素。注意,下一个元素不能与当前元素相同,否则就会出现重复的组合。
  5. 如果下一个元素不满足条件,就回溯到上一个元素,继续搜索下一个元素。

这个思路可以用一个dfs函数来实现,其中参数candidates表示数组,target表示目标值,index表示从哪个位置开始搜索,path表示当前搜索的路径,res表示结果集。

在dfs函数中,先判断target是否为0,如果是0,就找到一组解,将其添加到结果集中,然后返回;如果target小于当前元素,那么就剪枝,跳出循环;否则,将当前元素添加到路径中,继续搜索下一个元素,从i+1开始,因为当前元素不能重复使用,最后回溯。同时,需要注意去重的问题,如果下一个元素与当前元素相同,就跳过,避免出现重复的组合。