lllrc 2020-08-29 23:55 采纳率: 0%
浏览 109
已采纳

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操作不会影响结果
            }
        }
}

  • 写回答

3条回答 默认 最新

  • Json-Huang 2020-08-30 09:08
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已采纳回答 8月29日

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动