Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3918d8e

Browse files
update: 131
1 parent 59c1236 commit 3918d8e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
8181
| 127 | [Word Ladder](https://leetcode.com/problems/word-ladder/) | [JavaScript](./src/word-ladder/res.js) | Medium |
8282
| 128 | [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/) | [JavaScript](./src/longest-consecutive-sequence/res.js) | Medium |
8383
| 130 | [Surrounded Regions](https://leetcode.com/problems/surrounded-regions/) | [JavaScript](./src/surrounded-regions/res.js) | Medium |
84+
| 131 | [palindrome-partitioning](https://leetcode.com/problems/palindrome-partitioning/) | [TypeScript](./src/palindrome-partitioning/res.ts) | Medium |
8485
| 133 | [Clone Graph](https://leetcode.com/problems/clone-graph/) | [JavaScript](./src/clone-graph/res.js) | Medium |
8586
| 134 | [Gas Station](https://leetcode.com/problems/gas-station/) | [JavaScript](./src/gas-station/res.js) | Medium |
8687
| 135 | [Candy](https://leetcode.com/problems/candy/) | [JavaScript](./src/candy/res.js) | Hard |

‎src/palindrome-partitioning/res.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function partition(s: string): string[][] {
2+
const result = [];
3+
let answers = [];
4+
5+
const dfs = (i:number) => {
6+
if (i === s.length) {
7+
result.push(answers.slice());
8+
return ;
9+
}
10+
11+
for (let j = i; j < s.length; j++) {
12+
if (reverseCache[i][j]) {
13+
answers.push(s.slice(i, j+1));
14+
dfs(j+1);
15+
answers.pop();
16+
}
17+
}
18+
}
19+
20+
const reverseCache = new Array(s.length).fill([]).map(() => new Array(s.length).fill(true));
21+
for (let i = reverseCache.length; i >= 0; i--) {
22+
for (let j = i; j < reverseCache.length; j++) {
23+
if (i === j) {
24+
reverseCache[i][j] = true;
25+
} else if (i + 1 === j) {
26+
reverseCache[i][j] = s[i] === s[j];
27+
} else {
28+
reverseCache[i][j] = reverseCache[i+1][j-1] && s[i] === s[j];
29+
}
30+
}
31+
}
32+
33+
dfs(0);
34+
35+
return result;
36+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /