组合总和 II

题目形容:给定一个数组 candidates 和一个指标数 target ,找出 candidates 中所有能够使数字和为 target 的组合。

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

阐明:

  • 所有数字(包含指标数)都是正整数。
  • 解集不能蕴含反复的组合。

示例阐明请见LeetCode官网。

起源:力扣(LeetCode)
链接:https://leetcode-cn.com/probl...
著作权归领扣网络所有。商业转载请分割官网受权,非商业转载请注明出处。

解法一:回溯算法
  • 首先,将原数组排序;
  • 而后,申明一个数组freq用来记录每个不同的数字呈现的次数;
  • 而后,用回溯算法递归判断解决的序列是否符合条件,将符合条件的序列增加到后果集中,最初返回所有符合条件的后果集。
import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class LeetCode_040 {    /**     * 记录每个不同的数字呈现的次数     */    private static List<int[]> freq = new ArrayList<>();    /**     * 符合条件的后果集     */    private static List<List<Integer>> ans = new ArrayList<>();    /**     * 匹配的序列     */    private static List<Integer> sequence = new ArrayList<>();    /**     * 回溯算法     *     * @param candidates     * @param target     * @return     */    public static List<List<Integer>> combinationSum2(int[] candidates, int target) {        // 首先将给定的数组排序        Arrays.sort(candidates);        for (int num : candidates) {            int size = freq.size();            if (freq.isEmpty() || num != freq.get(size - 1)[0]) {                freq.add(new int[]{num, 1});            } else {                ++freq.get(size - 1)[1];            }        }        dfs(0, target);        return ans;    }    public static void dfs(int pos, int rest) {        if (rest == 0) {            /**             * 如果以后的和曾经等于target了,将以后的序列增加到后果集             */            ans.add(new ArrayList<Integer>(sequence));            return;        }        if (pos == freq.size() || rest < freq.get(pos)[0]) {            /**             * 如果曾经遍历完所有的数字或者待处理的数小于下一个要匹配的数字,则以后的序列不符合条件,返回             */            return;        }        dfs(pos + 1, rest);        int most = Math.min(rest / freq.get(pos)[0], freq.get(pos)[1]);        for (int i = 1; i <= most; ++i) {            sequence.add(freq.get(pos)[0]);            dfs(pos + 1, rest - i * freq.get(pos)[0]);        }        for (int i = 1; i <= most; ++i) {            sequence.remove(sequence.size() - 1);        }    }    public static void main(String[] args) {        int[] candidates = new int[]{10, 1, 2, 7, 6, 1, 5};        for (List<Integer> integers : combinationSum2(candidates, 8)) {            for (Integer integer : integers) {                System.out.print(integer + " ");            }            System.out.println();        }    }}
【每日寄语】 登高望远,不是为了被整个世界看到,而是为了看到整个世界。