-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy path842. Split Array into Fibonacci Sequence.go
64 lines (60 loc) · 1.57 KB
/
842. Split Array into Fibonacci Sequence.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
61
62
63
64
package leetcode
import (
"strconv"
"strings"
)
func splitIntoFibonacci(S string) []int {
if len(S) < 3 {
return []int{}
}
res, isComplete := []int{}, false
for firstEnd := 0; firstEnd < len(S)/2; firstEnd++ {
if S[0] == '0' && firstEnd > 0 {
break
}
first, _ := strconv.Atoi(S[:firstEnd+1])
if first >= 1<<31 { // 题目要求每个数都要小于 2^31 - 1 = 2147483647,此处剪枝很关键!
break
}
for secondEnd := firstEnd + 1; max(firstEnd, secondEnd-firstEnd) <= len(S)-secondEnd; secondEnd++ {
if S[firstEnd+1] == '0' && secondEnd-firstEnd > 1 {
break
}
second, _ := strconv.Atoi(S[firstEnd+1 : secondEnd+1])
if second >= 1<<31 { // 题目要求每个数都要小于 2^31 - 1 = 2147483647,此处剪枝很关键!
break
}
findRecursiveCheck(S, first, second, secondEnd+1, &res, &isComplete)
}
}
return res
}
// Propagate for rest of the string
func findRecursiveCheck(S string, x1 int, x2 int, left int, res *[]int, isComplete *bool) {
if x1 >= 1<<31 || x2 >= 1<<31 { // 题目要求每个数都要小于 2^31 - 1 = 2147483647,此处剪枝很关键!
return
}
if left == len(S) {
if !*isComplete {
*isComplete = true
*res = append(*res, x1)
*res = append(*res, x2)
}
return
}
if strings.HasPrefix(S[left:], strconv.Itoa(x1+x2)) && !*isComplete {
*res = append(*res, x1)
findRecursiveCheck(S, x2, x1+x2, left+len(strconv.Itoa(x1+x2)), res, isComplete)
return
}
if len(*res) > 0 && !*isComplete {
*res = (*res)[:len(*res)-1]
}
return
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}