leetcode 40题 有一步骤实在不懂

力扣

为什么判断不能重复利用的时候是i > cur?不是cur每次都会增加吗

class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        List<Integer> path = new ArrayList<Integer>();
        getResult(candidates, 0, target, path, result);

        return result;
    }

    private void getResult(int[] candidates, int cur, int target, List<Integer> path, List<List<Integer>> result){
            if (target > 0) {
                for (int i = cur; i < candidates.length; i++) {
                    if (i > cur && candidates[i] == candidates[i-1]) continue;
                    path.add(path.size(), candidates[i]);  // 尾插
                    getResult(candidates, i+1, target - candidates[i], path, result);
                    path.remove(path.size()-1); // 删除最后一个元素
                }
            } 
            if (target == 0) {
                result.add(new ArrayList(path));  // 重新赋值给一个path 后面对path操作不会影响结果
            }
        }
}

cur是为传入的变量值,后面没有给cur进行赋值语句,不会变化,自增的是i

https://blog.csdn.net/qq_42167528/article/details/89298862

i > cur && candidates[i] == candidates[i-1]

这行代码的作用不是用来排除重复利用问题,而是用来避免出现重复结果,i>cur是为了避免数组越界