mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Merge pull request #1172 from xiaofei-2020/back04
添加(0216.组合总和III.md):增加typescript版本
This commit is contained in:
@ -396,6 +396,30 @@ var combinationSum3 = function(k, n) {
|
||||
};
|
||||
```
|
||||
|
||||
## TypeScript
|
||||
|
||||
```typescript
|
||||
function combinationSum3(k: number, n: number): number[][] {
|
||||
const resArr: number[][] = [];
|
||||
function backTracking(k: number, n: number, sum: number, startIndex: number, tempArr: number[]): void {
|
||||
if (sum > n) return;
|
||||
if (tempArr.length === k) {
|
||||
if (sum === n) {
|
||||
resArr.push(tempArr.slice());
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (let i = startIndex; i <= 9 - (k - tempArr.length) + 1; i++) {
|
||||
tempArr.push(i);
|
||||
backTracking(k, n, sum + i, i + 1, tempArr);
|
||||
tempArr.pop();
|
||||
}
|
||||
}
|
||||
backTracking(k, n, 0, 1, []);
|
||||
return resArr;
|
||||
};
|
||||
```
|
||||
|
||||
## C
|
||||
|
||||
```c
|
||||
|
Reference in New Issue
Block a user