File tree 2 files changed +41
-0
lines changed
2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 72
72
| 124| [ 二叉树中的最大路径和] ( https://leetcode.cn/problems/binary-tree-maximum-path-sum/ ) | [ JavaScript] ( ./algorithms/binary-tree-maximum-path-sum.js ) | Hard|
73
73
| 125| [ 验证回文串] ( https://leetcode.cn/problems/valid-palindrome/ ) | [ JavaScript] ( ./algorithms/valid-palindrome.js ) | Easy|
74
74
| 129| [ 求根节点到叶节点数字之和] ( https://leetcode.cn/problems/sum-root-to-leaf-numbers/ ) | [ JavaScript] ( ./algorithms/sum-root-to-leaf-numbers.js ) | Medium|
75
+ | 131| [ 分割回文串] ( https://leetcode.cn/problems/palindrome-partitioning/ ) | [ JavaScript] ( ./algorithms/palindrome-partitioning.js ) | Medium|
75
76
| 136| [ 只出现一次的数字] ( https://leetcode-cn.com/problems/single-number/ ) | [ JavaScript] ( ./algorithms/single-number.js ) | Easy|
76
77
| 141| [ 环形链表] ( https://leetcode-cn.com/problems/linked-list-cycle/ ) | [ JavaScript] ( ./algorithms/linked-list-cycle.js ) | Easy|
77
78
| 142| [ 环形链表 II] ( https://leetcode.cn/problems/linked-list-cycle-ii/ ) | [ JavaScript] ( ./algorithms/linked-list-cycle-ii.js ) | Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 131. 分割回文串
3
+ * @param {string } s
4
+ * @return {string[][] }
5
+ */
6
+ var partition = function ( s ) {
7
+ const result = [ ] ;
8
+ const path = [ ] ;
9
+
10
+ const backtracking = ( s , startIndex = 0 ) => {
11
+ if ( startIndex >= s . length ) {
12
+ result . push ( path . slice ( ) ) ;
13
+ return ;
14
+ }
15
+ for ( let i = startIndex ; i < s . length ; i ++ ) {
16
+ if ( isPalindrome ( s , startIndex , i ) ) {
17
+ const str = s . slice ( startIndex , i + 1 ) ;
18
+ path . push ( str ) ;
19
+ } else {
20
+ continue ;
21
+ }
22
+ backtracking ( s , i + 1 ) ;
23
+ path . pop ( ) ;
24
+ }
25
+ } ;
26
+ backtracking ( s ) ;
27
+
28
+ return result ;
29
+ } ;
30
+
31
+ function isPalindrome ( s , left , right ) {
32
+ while ( left < right ) {
33
+ if ( s [ left ] !== s [ right ] ) {
34
+ return false ;
35
+ }
36
+ left ++ ;
37
+ right -- ;
38
+ }
39
+ return true ;
40
+ }
You can’t perform that action at this time.
0 commit comments