-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (53 loc) · 1.24 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import "fmt"
// 判断是否是回文
func isPalindrome(s string) bool {
sRune := []rune(s)
i := 0
j := len(sRune) - 1
for i < j {
if s[i] != s[j] {
return false
}
i++
j--
}
return true
}
var (
res = make([][]string, 0) // 存放最终结果
path = make([]string, 0) // 存放中间结果
)
func backtracking(s string, startIndex int) {
sRune := []rune(s)
// 如果起始位置已经大于s的大小,说明已经找到了一组分割方案了
if startIndex >= len(sRune) {
temp := make([]string, len(path))
copy(temp, path)
res = append(res, temp)
return
}
for i := startIndex; i < len(sRune); i++ {
partS := sRune[startIndex : i+1] // 从[startIndex, i]这部分区域
// 是回文
if isPalindrome(string(partS)) {
path = append(path, string(partS))
} else {
continue
}
backtracking(s, i+1) // 寻找i+1为起始位置的子串
path = path[:len(path)-1] // 回溯过程,弹出本次已经填在的子串
}
}
func partition(s string) [][]string {
res = make([][]string, 0)
path = make([]string, 0)
backtracking(s, 0)
return res
}
func main() {
println("UseCase 1......")
fmt.Printf("%+v\n", partition("aab"))
println("UseCase 2......")
fmt.Printf("%+v\n", partition("a"))
}