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