解析思路

leetcode 中等难度,题目描述点击这里

本题属于组合排序的进阶题型。建议先看上一篇的解析。

本题跟上一题区别只有一点是选择的数字不能重复,如何实现不重复呢?

很简单,只需要将传入的 index 设置成 index+1,同时 for 循环时每次都循环到下一个不同的元素上

这样就不会选取到已经选过的元素。

代码如下:

代码

public class Q40 {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(candidates);
        dfs(candidates, target, 0, new Stack<>(), res);
        return res;
    }

    private void dfs(int[] candidates, int target, int index, Stack<Integer> temp, List<List<Integer>> res) {
        if (target == 0) {
            //说明找到一个结果序列
            res.add(new ArrayList<>(temp));
            return;
        }
        for (int i = index; i < candidates.length; ) {
            if (candidates[i] > target) {
                //前面已经排序过,所以在这里可以进行剪枝操作,如果candidates[index]都小于target了,那就不需要比较后面的了,肯定不满足要求
                return;
            }
            temp.push(candidates[i]);
            dfs(candidates, target - candidates[i], i + 1, temp, res);
            temp.pop();
            //手动控制i的增长,对于同一个数字不能重复处理
            int nextI = i + 1;
            while (nextI < candidates.length && candidates[nextI] == candidates[i]) {
                nextI++;
            }
            i = nextI;
        }
    }

    public static void main(String[] args) {
        new Q40().combinationSum2(new int[]{10, 1, 2, 7, 6, 1, 5}, 8).forEach(System.out::println);
    }

}