39. Combination Sum (Java)

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

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

Note:

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

Example 1:

Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
  [7],
  [2,2,3]
]

Example 2:

Input: candidates = [2,3,5], target = 8,
A solution set is:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]
class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<Integer> ans = new ArrayList<Integer>();
        Arrays.sort(candidates);
        backTrack(candidates, target, 0, ans, 0);
        return result;
    }

    public void backTrack(int[] candidates, int target, int start, List<Integer> ans, int sum){
        if(start >= candidates.length || sum + candidates[start] > target)
            return; //not found
        else if(sum + candidates[start] == target ){ //found an answer
            List<Integer> new_ans = new ArrayList<Integer>(ans); //不能用List<Integer> new_ans = ans;这个只是创建了原List的一个引用
            new_ans.add(candidates[start]);
            result.add(new_ans);
        }
        else{
            // not choose current candidate
            backTrack(candidates,target,start+1,ans,sum);

            //choose current candidate
            ans.add(candidates[start]);
            backTrack(candidates,target,start,ans,sum+candidates[start]);
            ans.remove(ans.size()-1); //List是按引用传递,为了不影响递归,需要复原
        }
    }

    private List<List<Integer>> result = new ArrayList<List<Integer>>();
}

注意List按引用(地址)传递,需要新new一个,或是复原,以不影响其他部分

原文地址:https://www.cnblogs.com/qionglouyuyu/p/10812822.html

时间: 2024-11-09 04:32:42

39. Combination Sum (Java)的相关文章

39. Combination Sum java solutions

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

[LeetCode] Add to List 39. Combination Sum Java

题目:Given a set of candidate numbers (C) (without duplicates) 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

[array] leetcode - 39. Combination Sum - Medium

leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without duplicates) 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

刷题39. Combination Sum

一.题目说明 题目39. Combination Sum,是从正数列表中选取几个,其和等于目标数的可能组合.任何一个数可以重复取,如candidates = [2,3,6,7], target = 7,结果集合是[ [7], [2,2,3] ] 如candidates = [2,3,5], target = 8,结果集合是 [ [2,2,2,2], [2,3,3], [3,5] ] 题目难度是Medium,先思考一下,再来解答. 二.我的解答 经过一番思考,这个题目可以通过dfs(树的深度优先遍

[Leetcode][Python]39: Combination Sum

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 39: Combination Sumhttps://oj.leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a target number (T),find all unique combinations in C where the candidate numbers

leetCode 39.Combination Sum(组合总和) 解题思路和方法

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 (includi

39. Combination Sum I/II

基础backtracing题,先排序一下,每次传一个参数表示开始的下标. class Solution { public: vector<vector<int>> res; vector<vector<int>> combinationSum(vector<int>& candidates, int target) { sort(candidates.begin(),candidates.end()); vector<int>

Java [Leetcode 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)

LeetCode 39. Combination Sum (组合的和)

Given a set of candidate numbers (C) (without duplicates) 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 (in