File tree Expand file tree Collapse file tree 4 files changed +75
-0
lines changed Expand file tree Collapse file tree 4 files changed +75
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {number[][] }
4+ */
5+ let permute = function ( nums ) {
6+ let n = nums . length
7+ if ( n === 1 ) {
8+ return [ nums ]
9+ }
10+
11+ let res = [ ]
12+ for ( let i = 0 ; i < n ; i ++ ) {
13+ let use = nums [ i ]
14+ let rest = nums . slice ( 0 , i ) . concat ( nums . slice ( i + 1 , n ) )
15+ let restPermuteds = permute ( rest )
16+ for ( let restPermuted of restPermuteds ) {
17+ res . push ( restPermuted . concat ( use ) )
18+ }
19+ }
20+
21+ return res
22+ }
23+
24+ console . log ( permute ( [ 1 , 2 , 3 ] ) )
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @return {string[][] }
4+ */
5+
6+ let partition = function ( s ) {
7+ let n = s . length
8+ let ret = [ ]
9+ let find = function ( start , prev ) {
10+ // 最少分割一个字符 最多分割到末尾前一位
11+ for ( let i = 1 ; i <= n ; i ++ ) {
12+ let end = start + i
13+ let cur = s . substring ( start , end )
14+ if ( cur ) {
15+ let res = prev . concat ( cur )
16+ if ( isPalindrome ( cur ) ) {
17+ if ( end === n ) {
18+ ret . push ( res )
19+ } else {
20+ find ( start + i , res )
21+ }
22+ }
23+ }
24+ }
25+ }
26+ find ( 0 , [ ] )
27+ return ret
28+ }
29+
30+ function isPalindrome ( s ) {
31+ if ( ! s ) {
32+ return false
33+ }
34+ let i = 0
35+ let j = s . length - 1
36+
37+ while ( i < j ) {
38+ let head = s [ i ]
39+ let tail = s [ j ]
40+
41+ if ( head !== tail ) {
42+ return false
43+ } else {
44+ i ++
45+ j --
46+ }
47+ }
48+ return true
49+ }
50+
51+ console . log ( partition ( "aab" ) )
File renamed without changes.
File renamed without changes.
You can’t perform that action at this time.
0 commit comments