[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 (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]
]

题意及分析:给出一个数组和一个目标量target,求出由数组元素组成的数组和等于target的所有可能(数组中的元素可以复用),数组所有元素都为正整数,不能包含相同的数组。这道题要求出所有可能,所以可以使用回溯的方法。我们用一个start变量存储子数组开始的位置,用int remian保留target减去当前求值数组的和,这样我们就可以转化为 在start和candidates.length-1中求 和等于target的数组。理解不够深入,讲得不是很清楚,具体可以看一下代码。。

代码:

public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        List<Integer> array= new ArrayList<>();
        List<List<Integer>> list = new ArrayList<>();
        int start=0;
        int remain=target;
        backTracking(list,array,candidates,start,remain);
        return list;
    }

	public void backTracking(List<List<Integer>> list,List<Integer> array,int[] candidates,int start,int remain) {
		if(remain<0)
			return;
		else if(0==remain){	//有解
			list.add(new ArrayList<>(array));
		}else{
			for(int i=start;i<candidates.length;i++){
				if(i>0&&candidates[i]==candidates[i-1]) continue;  //这一步操作主要是去重
				array.add(candidates[i]);
				backTracking(list, array, candidates, i, remain-candidates[i]);    //这里注意一下是i,因为元素可以重复利用
				array.remove(array.size()-1);
			}
		}
		return;
	}
}

  

时间: 2024-10-04 21:49:58

[LeetCode] Add to List 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

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 n

[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

[Leetcode 40]组合数和II Combination Sum II

[题目] Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates may only be used once in the combination. Note: A

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)