From 4959bd4c8aa78d401f5a1f14c2d267a4d046baf5 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 28 Mar 2022 17:15:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880039.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8C.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\220\210\346\200\273\345\222\214.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index 7a2084dd19..98b37b8467 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -392,7 +392,34 @@ var combinationSum = function(candidates, target) { }; ``` +## TypeScript + +```typescript +function combinationSum(candidates: number[], target: number): number[][] { + const resArr: number[][] = []; + function backTracking( + candidates: number[], target: number, + startIndex: number, route: number[], curSum: number + ): void { + if (curSum > target) return; + if (curSum === target) { + resArr.push(route.slice()); + return + } + for (let i = startIndex, length = candidates.length; i < length; i++) { + let tempVal: number = candidates[i]; + route.push(tempVal); + backTracking(candidates, target, i, route, curSum + tempVal); + route.pop(); + } + } + backTracking(candidates, target, 0, [], 0); + return resArr; +}; +``` + ## C + ```c int* path; int pathTop;