zoukankan      html  css  js  c++  java
  • Leetcode39.组合总和

    题目连接:39.组合总和

    思路:暴力遍历。先对数组排序,在进行枚举。

    代码:

    class Solution {
        private List<List<Integer>> res = new ArrayList<>();
        public List<List<Integer>> combinationSum(int[] candidates, int target) {
            Arrays.sort(candidates);
            helper(candidates, target, 0, new ArrayList<>());
            return res;
        }
        private void helper(int[] c, int t, int idx, List<Integer> list){
            if(t == 0){
                res.add(new ArrayList<>(list));
            }
            for(int i=idx; i<c.length; i++){
                if(t - c[i] < 0) return ;
                list.add(c[i]);
                helper(c, t - c[i], i, list);
                list.remove(list.size()-1);
            }
        }
    }
    

    执行用时:2 ms, 在所有 Java 提交中击败了99.92%的用户
    内存消耗:38.5 MB, 在所有 Java 提交中击败了85.95%的用户

  • 相关阅读:
    原型污染
    C#之抛异常
    为什么['1', '7', '11'].map(parseInt) returns [1, NaN, 3]?
    Linux
    Linux
    Linux
    Linux
    Linux
    Linux
    其他
  • 原文地址:https://www.cnblogs.com/liuyongyu/p/14201270.html
Copyright © 2011-2022 走看看