-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCR086-PalindromePartitioning.go
More file actions
107 lines (97 loc) · 2.79 KB
/
LCR086-PalindromePartitioning.go
File metadata and controls
107 lines (97 loc) · 2.79 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
// LCR 086. 分割回文串
// 给定一个字符串 s ,请将 s 分割成一些子串,使每个子串都是 回文串 ,返回 s 所有可能的分割方案。
// 回文串 是正着读和反着读都一样的字符串。
// 示例 1:
// 输入:s = "google"
// 输出:[["g","o","o","g","l","e"],["g","oo","g","l","e"],["goog","l","e"]]
// 示例 2:
// 输入:s = "aab"
// 输出:[["a","a","b"],["aa","b"]]
// 示例 3:
// 输入:s = "a"
// 输出:[["a"]]
// 提示:
// 1 <= s.length <= 16
// s 仅由小写英文字母组成
import "fmt"
import "time"
// DFS 递归求解
func partition(s string) [][]string {
res := [][]string{}
size := len(s)
if size == 0 {
return res
}
// 判断是否是回文
isPalindrome := func (str string, start, end int) bool {
for start < end {
if str[start] != str[end] {
return false
}
start++
end--
}
return true
}
var dfs func(s string, idx int, cur []string, result *[][]string)
dfs = func(s string, idx int, cur []string, result *[][]string) {
start, end := idx, len(s)
if start == end {
temp := make([]string, len(cur))
copy(temp, cur)
*result = append(*result, temp)
return
}
for i := start; i < end; i++ {
if isPalindrome(s, start, i) { // 只处理回文的情况
dfs(s, i+1, append(cur, s[start:i+1]), result)
}
}
}
current := make([]string, 0, size)
dfs(s, 0, current, &res)
return res
}
// best solution
func partition1(s string) [][]string {
path, res := []string{}, [][]string {}
check := func(i, j int) bool {
for i < j {
if s[i] != s[j] {
return false
}
i++
j--
}
return true
}
var dfs func(start int)
dfs = func(start int) {
if start >= len(s) {
tmp := make([]string, len(path))
copy(tmp, path)
res = append(res, tmp)
return
}
for i := start; i < len(s); i++ {
if check(start, i) {
path = append(path, s[start:i+1])
dfs(i + 1)
path = path[:len(path) - 1]
}
}
}
dfs(0)
return res
}
func main() {
start := time.Now() // 获取当前时间
fmt.Println(partition("aab")) // [[a a b] [aa b]]
fmt.Println(partition("a")) // [[a]]
fmt.Printf("ladderLength use : %v \r\n",time.Since(start))
start = time.Now() // 获取当前时间
fmt.Println(partition1("aab")) // [[a a b] [aa b]]
fmt.Println(partition1("a")) // [[a]]
fmt.Printf("ladderLength use : %v \r\n",time.Since(start))
}