mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1183 from xiaofei-2020/back09
添加(0131.分割回文串.md):增加typescript版本
This commit is contained in:
@ -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
|
||||||
|
Reference in New Issue
Block a user