216. Combination Sum III java solutions

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.

Example 1:

Input: k = 3, n = 7

Output:

[[1,2,4]]

Example 2:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

 1 public class Solution {
 2     public List<List<Integer>> combinationSum3(int k, int n) {
 3         ArrayList<List<Integer>> ans = new ArrayList<List<Integer>>();
 4         ArrayList<Integer> tmp = new ArrayList<Integer>();
 5         if(k < 1 || n < 1) return ans;
 6         combinationDFS(ans, tmp, k, n, 1, 0);
 7         return ans;
 8     }
 9
10     public void combinationDFS(ArrayList<List<Integer>> ans, ArrayList<Integer> tmp, int k, int n, int level, int sum){
11         if(sum == n && k == 0){
12             ans.add(new ArrayList(tmp));
13             return;
14         }else if(sum > n || k < 0){
15             return;
16         }
17         for(int i = level; i<=9; i++){
18             tmp.add(i);
19             combinationDFS(ans,tmp,k-1,n,i+1,sum+i);
20             tmp.remove(tmp.size()-1);
21         }
22     }
23 }
时间: 2024-10-27 06:43:50

216. Combination Sum III java solutions的相关文章

【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

Java for LeetCode 216 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 216. Combination Sum III 求和III ---------- java

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. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n

Leetcode 216. 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. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n

LeetCode 216. 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. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n

[leedcode 216] 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

216 Combination Sum III

public class Combination_Sum_III { /** * 思路: * 只能使用1~9 * 例如: * 给定n=9 k=3,说明用3个数组成的和要等于9 * 当sum不等于9时,一直往后走: * [123]!=9 ==> [124]!=9 ==> [125]!=9 * 当sum等于9时,返回找下一个可能 * [126]=9 (原因:再往后走,就会比9大了,因为是递增的数列) * ==> [134]... * ---------------数据结构----------

216. Combination Sum III——本质DFS

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. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n

216. 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. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n