zoukankan      html  css  js  c++  java
  • 39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

    The same repeated number may be chosen from C unlimited number of times.

    Note:

    • All numbers (including target) will be positive integers.
    • The solution set must not contain duplicate combinations.

    For example, given candidate set [2, 3, 6, 7] and target 7
    A solution set is: 

    [
      [7],
      [2, 2, 3]
    ]
    public class Solution {
        public List<List<Integer>> combinationSum(int[] candidates, int target) {
            List<List<Integer>> res = new ArrayList<>();
            List<Integer> member = new ArrayList<>();
            if(candidates.length == 0)
                return res;
            dfs(res, member, candidates, target, 0);
            return res;
        }
        public void dfs(List<List<Integer>> res, List<Integer> member, int[] candidates, int target, int deep){
            if(target < 0)
                return;
            if(target == 0){
                res.add(new ArrayList<Integer>(member));
                return;
            }
            for(int i = deep; i < candidates.length; i++){
                member.add(candidates[i]);
                dfs(res, member, candidates, target-candidates[i], i);
                member.remove(member.size()-1);
            }
        }
    }
  • 相关阅读:
    COGS 859. 数列
    9.7noip模拟试题
    hash练习
    9.6noip模拟试题
    9.5noip模拟试题
    poj 2117 Electricity
    洛谷P1993 小 K 的农场(查分约束)
    9.2noip模拟试题
    洛谷 P1273 有线电视网(dp)
    面试题收集
  • 原文地址:https://www.cnblogs.com/joannacode/p/6072267.html
Copyright © 2011-2022 走看看