Merge pull request #1183 from xiaofei-2020/back09

添加(0131.分割回文串.md):增加typescript版本
This commit is contained in:
程序员Carl
2022-04-14 10:14:38 +08:00
committed by GitHub

View File

@ -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
```c ```c