Leetcode: 39. Combination Sum

博客围绕组合求和问题展开,包括给定候选数字集和目标数,找出和为目标数的唯一组合,同一数字可重复或仅用一次的情况。还提及类似篮球得分组合、硬币组合求和等问题,并给出递归求解思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Question

Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidateswhere 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]
]

Solution

class Solution {
	private List<List<Integer>> res;
	public List<List<Integer>> combinationSum(int[] candidates, int target) {
		this.res = new LinkedList<List<Integer>>();
		ArrayList<Integer> store = new ArrayList<Integer>();
		dfs(store, candidates, target, 0);
		return res;
	}
	public void dfs(ArrayList<Integer> store, int[] candidates, int target, int star) {
		if (target == 0) {
			this.res.add((List<Integer>)store.clone());
			return;
		}
		if (target < 0) {
			return;
		}
		for (int i = star; i < candidates.length; i++) {
			store.add(candidates[i]);
			dfs(store, candidates, target - candidates[i], i);
			store.remove(store.size()-1);
		}
		// why i should start form star instead of just 0?
		// because in that way, there'll be infinite loop
	}
}

Similar Problem

You can win three kinds of basketball points, 1 point, 2 points, and 3 points. Given a total score n, print out all the combination to compose n.

  1. At first position we can have three numbers 1 or 2 or 3.
  2. First put 1 at first position and recursively call for n-1.
  3. Then put 2 at first position and recursively call for n-2.
  4. Then put 3 at first position and recursively call for n-3.
  5. If n becomes 0 then we have formed a combination that compose n, so print the current combination.

Combine Sum II

Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidateswhere the candidate numbers sums to target.
Each number in candidatesmay only be used once in the combination.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:

Input: candidates = [10,1,2,7,6,1,5], target = 8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

class Solution {
	private List<List<Integer>> res;
	public List<List<Integer>> combinationSum2(int[] candidates, int target) {
		this.res = new LinkedList<List<Integer>>(); // here we must use LinkedList becasue List is an abstract structure
		ArrayList<Integer> store = new ArrayList<Integer>();
		Arrays.sort(candidates); // why we must sort it before the dfs?
	}
	public viod dfs(int[] candidates, int target, ArrayList<Integer> store, int star){
		if (target == 0) {
			this.res.add((List<Integer>)store.clone());
			return;
		}
		if (target < 0) {
			return;
		}
		for (int i = star; i < candidates.length; i++) {
			if (candidates[i] > target) {
				break;
			}
			store.add(candidates[i]);
			dfs(candidates, target - candidates[i], store, i + 1);
			// here we must use i + 1 instead of i++
			store.remove(store.size()-1);
			while (i + 1 < candiates.length && candidates[i] == candidates[]) {
				i++;
			}
		}
	}
}

Extra thinking…

Print all combination of coins that can sum up to a total value k
E.g. total value k = 99 cents
coin value = 25, 10, 5, 1 cent

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值