class Solution {
static List<List<Integer>> result;
static List<Integer> list;
static int limit;
static int sum;
public List<List<Integer>> combinationSum3(int k, int n) {
result = new ArrayList<>();
list = new ArrayList<>();
for(int i = 0; i < k; i++) {
list.add(0);
}
limit = k;
sum = n;
combination(0, 0, 1);
return result;
}
public void combination(int s, int n, int index) {
if (n == limit) {
if (s == sum) {
result.add(new ArrayList<>(list));
}
return;
}
for(int i = index; i < 10; i++) {
list.set(n, i);
combination(s + i, n + 1, i + 1);
}
}
}
'코딩테스트' 카테고리의 다른 글
[leetcode] 1456.Maximum Number of Vowels in a Substring of Given Length (0) | 2024.06.24 |
---|---|
[leetcode] 2336. Smallest Number in Infinite Set (0) | 2024.06.18 |
[leetcode] 994. Rotting Oranges (0) | 2024.06.14 |
[leetcode] 1161. Maximum Level Sum of a Binary Tree (0) | 2024.06.14 |
[leetcode] 1448. Count Good Nodes in Binary Tree (0) | 2024.06.14 |