From f0b771af5c8d1fe5d5a298f00a2dbc05a1e23b42 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Wed, 30 Mar 2022 16:40:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880131.=E5=88=86?= =?UTF-8?q?=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=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/0131.分割回文串.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index f50f1c1d..10b747cb 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -450,6 +450,38 @@ var partition = function(s) { }; ``` +## TypeScript + +```typescript +function partition(s: string): string[][] { + function isPalindromeStr(s: string, left: number, right: number): boolean { + while (left < right) { + if (s[left++] !== s[right--]) { + return false; + } + } + return true; + } + function backTracking(s: string, startIndex: number, route: string[]): void { + let length: number = s.length; + if (length === startIndex) { + resArr.push(route.slice()); + return; + } + for (let i = startIndex; i < length; i++) { + if (isPalindromeStr(s, startIndex, i)) { + route.push(s.slice(startIndex, i + 1)); + backTracking(s, i + 1, route); + route.pop(); + } + } + } + const resArr: string[][] = []; + backTracking(s, 0, []); + return resArr; +}; +``` + ## C ```c