@@ -454,31 +454,36 @@ var partition = function(s) {
454454
455455``` typescript
456456function partition(s : string ): string [][] {
457- function isPalindromeStr(s : string , left : number , right : number ): boolean {
458- while (left < right ) {
459- if (s [left ++ ] !== s [right -- ]) {
460- return false ;
457+ const res: string [][] = []
458+ const path: string [] = []
459+ const isHuiwen = (
460+ str : string ,
461+ startIndex : number ,
462+ endIndex : number
463+ ): boolean => {
464+ for (; startIndex < endIndex ; startIndex ++ , endIndex -- ) {
465+ if (str [startIndex ] !== str [endIndex ]) {
466+ return false
461467 }
462468 }
463- return true ;
469+ return true
464470 }
465- function backTracking(s : string , startIndex : number , route : string []): void {
466- let length: number = s .length ;
467- if (length === startIndex ) {
468- resArr .push (route .slice ());
469- return ;
471+ const rec = (str : string , index : number ): void => {
472+ if (index >= str .length ) {
473+ res .push ([... path ])
474+ return
470475 }
471- for (let i = startIndex ; i < length ; i ++ ) {
472- if (isPalindromeStr (s , startIndex , i )) {
473- route .push (s .slice (startIndex , i + 1 ));
474- backTracking (s , i + 1 , route );
475- route .pop ();
476+ for (let i = index ; i < str .length ; i ++ ) {
477+ if (! isHuiwen (str , index , i )) {
478+ continue
476479 }
480+ path .push (str .substring (index , i + 1 ))
481+ rec (str , i + 1 )
482+ path .pop ()
477483 }
478484 }
479- const resArr: string [][] = [];
480- backTracking (s , 0 , []);
481- return resArr ;
485+ rec (s , 0 )
486+ return res
482487};
483488```
484489
0 commit comments