๐ 216. ็ปๅๆปๅ III
2022ๅนด6ๆ9ๆฅ
- algorithm
๐ 216. ็ปๅๆปๅ III
้พๅบฆ: ๐
้ฎ้ขๆ่ฟฐ
่งฃๆณ
class Solution {
List<List<Integer>> res = new LinkedList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
// ๆ่ทฏ๏ผ
// ๅๆบฏ + ๅชๆ
// ็นๆฎๆ
ๅต็นๅค
if(k >= n) {
return res;
}
LinkedList<Integer> path = new LinkedList<>();
mySol(k, n, 1, 0, path);
return res;
}
private void mySol(int k, int target, int index, int sum, LinkedList<Integer> path) {
// ้ๅฝ็ปๆญขๆกไปถ
if(path.size() == k) {
if(sum == target) {
res.add(new LinkedList<>(path));
}
return;
}
if(index > 9 || sum >= target) {
return;
}
// size() < k
for(int i = index; i <= 10 - k + path.size(); i ++) {
path.addLast(i);
mySol(k, target, i + 1, sum + i, path);
path.removeLast();
}
}
}