From a021470215aa859a4d5b0e9fe48dd775e6c0ed51 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 29 Mar 2022 14:38:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880040.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CII.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 --- problems/0040.组合总和II.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0040.组合总和II.md b/problems/0040.组合总和II.md index 49acb8d6..de13e031 100644 --- a/problems/0040.组合总和II.md +++ b/problems/0040.组合总和II.md @@ -532,6 +532,7 @@ var combinationSum2 = function(candidates, target) { }; ``` **使用used去重** + ```js var combinationSum2 = function(candidates, target) { let res = []; @@ -562,6 +563,37 @@ var combinationSum2 = function(candidates, target) { }; ``` +## TypeScript + +```typescript +function combinationSum2(candidates: number[], target: number): number[][] { + candidates.sort((a, b) => a - b); + const resArr: number[][] = []; + function backTracking( + candidates: number[], target: number, + curSum: number, startIndex: number, route: number[] + ) { + if (curSum > target) return; + if (curSum === target) { + resArr.push(route.slice()); + return; + } + for (let i = startIndex, length = candidates.length; i < length; i++) { + if (i > startIndex && candidates[i] === candidates[i - 1]) { + continue; + } + let tempVal: number = candidates[i]; + route.push(tempVal); + backTracking(candidates, target, curSum + tempVal, i + 1, route); + route.pop(); + + } + } + backTracking(candidates, target, 0, 0, []); + return resArr; +}; +``` + ## C ```c