zoukankan      html  css  js  c++  java
  • 039 Combination Sum 组合总和

    给一组候选数字(C)(没有重复)并给一个目标数字(T),找出 C 中所有唯一的组合使得它们的和为 T。
    可以从 C 无限次数中选择相同的数字。
    说明:
        所有数字(包括目标)都是正整数。
        解集合中没有相同组合。
    例如,给一个候选集合 [2, 3, 6, 7] 并且目标为 7,
    一个解的集合为:
    [
      [7],
      [2, 2, 3]
    ]

    详见:https://leetcode.com/problems/combination-sum/description/

    Java实现:

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

    参考:http://www.cnblogs.com/grandyang/p/4419259.html

  • 相关阅读:
    leetcode312 戳气球
    leetcode1283 使结果不超过阈值的最小除数
    软件管理相关命令
    win10+Tensorflow2.1+anaconda python3.7安装
    ResNet残差网络(可以解决梯度消失)
    梯度消失&梯度爆炸(Vanishing/exploding gradients)
    高方差和高偏差
    tf.nn.conv1d
    tensorboard
    卷积
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8688805.html
Copyright © 2011-2022 走看看