LeetCode Combination Sum III

原题链接在这里:https://leetcode.com/problems/combination-sum-iii/

Combination Sum II相似,不同的是中不是所有元素相加,只是k个元素相加。

所以在把item的copy 加到res前需要同时满足item.size() == k 和 target == 0两个条件。

AC Java:

 1 public class Solution {
 2     public List<List<Integer>> combinationSum3(int k, int n) {
 3         List<List<Integer>> res = new ArrayList<List<Integer>>();
 4         if(k <= 0 || n <=0){
 5             return res;
 6         }
 7         int [] candidates = {1,2,3,4,5,6,7,8,9};
 8         helper(k,n,0,candidates,new ArrayList<Integer>(),res);
 9         return res;
10     }
11     private void helper(int k, int n, int start, int [] candidates, List<Integer> item,  List<List<Integer>> res){
12         if(item.size() == k && n==0){
13             res.add(new ArrayList<Integer>(item));
14             return;
15         }
16         if(n<0 || item.size()>k){
17             return;
18         }
19         for(int i = start; i<candidates.length; i++){
20             item.add(candidates[i]);
21             helper(k,n-candidates[i],i+1,candidates,item,res);
22             item.remove(item.size()-1);
23         }
24     }
25 }
时间: 2024-12-19 08:10:57

LeetCode Combination Sum III的相关文章

[LeetCode] Combination Sum III 组合之和之三

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

[LeetCode][JavaScript]Combination Sum III

Combination Sum III Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascend

Leetcode题解(4):L216/Combination Sum III

L216: Combination Sum III Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in

【LeetCode】216. Combination Sum III

Combination Sum III Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascend

[leetcode]Combination Sum @ Python

原题地址:https://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 sums to T. The same repeated number may be chosen from C unlimited

LeetCode: Combination Sum [038]

[题目] 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: Combination Sum II [039]

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

Leetcode: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 (includ

Combination Sum,Combination Sum II,Combination Sum III

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