File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -374,6 +374,51 @@ class Solution:
374
374
return True
375
375
```
376
376
377
+ ## Go
378
+ ** 注意切片(go切片是披着值类型外衣的引用类型)**
379
+ ``` go
380
+ func partition (s string ) [][]string {
381
+ var tmpString []string // 切割字符串集合
382
+ var res [][]string // 结果集合
383
+ backTracking (s,tmpString,0 ,&res)
384
+ return res
385
+ }
386
+ func backTracking (s string ,tmpString []string ,startIndex int ,res *[][]string ){
387
+ if startIndex==len (s){// 到达字符串末尾了
388
+ // 进行一次切片拷贝,怕之后的操作影响tmpString切片内的值
389
+ t := make ([]string , len (tmpString))
390
+ copy (t, tmpString)
391
+ *res=append (*res,t)
392
+ }
393
+ for i := startIndex;i<len (s);i++{
394
+ // 处理(首先通过startIndex和i判断切割的区间,进而判断该区间的字符串是否为回文,若为回文,则加入到tmpString,否则继续后移,找到回文区间)(这里为一层处理)
395
+ if isPartition (s,startIndex,i){
396
+ tmpString=append (tmpString,s[startIndex:i+1 ])
397
+ }else {
398
+ continue
399
+ }
400
+ // 递归
401
+ backTracking (s,tmpString,i+1 ,res)
402
+ // 回溯
403
+ tmpString=tmpString[:len (tmpString)-1 ]
404
+ }
405
+ }
406
+ // 判断是否为回文
407
+ func isPartition (s string ,startIndex ,end int )bool {
408
+ left := startIndex
409
+ right := end
410
+ for ;left<right;{
411
+ if s[left]!=s[right]{
412
+ return false
413
+ }
414
+ // 移动左右指针
415
+ left++
416
+ right--
417
+ }
418
+ return true
419
+ }
420
+ ```
421
+
377
422
## javaScript
378
423
379
424
``` js
You can’t perform that action at this time.
0 commit comments