g0201_0300.s0216_combination_sum_iii.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0201_0300.s0216_combination_sum_iii;
// #Medium #Array #Backtracking #Udemy_Backtracking/Recursion
// #2022_07_02_Time_1_ms_(81.35%)_Space_41.8_MB_(46.36%)
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("java:S5413")
public class Solution {
public List> combinationSum3(int k, int n) {
List> res = new ArrayList<>();
solve(k, n, new ArrayList<>(), res, 0, 1);
return res;
}
private void solve(
int k, int target, List temp, List> res, int sum, int start) {
if (sum == target && temp.size() == k) {
res.add(new ArrayList<>(temp));
return;
}
if (temp.size() >= k) {
return;
}
if (sum > target) {
return;
}
for (int i = start; i <= 9; i++) {
temp.add(i);
solve(k, target, temp, res, sum + i, i + 1);
temp.remove(temp.size() - 1);
}
}
}