Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 2e3991e

Browse files
aQuaaQua
aQua
authored and
aQua
committed
842 accepted
1 parent cef908d commit 2e3991e

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

.vscode/bookmarks.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"onDidClearBookmarkEmitter": {},
3+
"onDidClearAllBookmarksEmitter": {},
4+
"onDidAddBookmarkEmitter": {},
5+
"onDidRemoveBookmarkEmitter": {},
6+
"onDidUpdateBookmarkEmitter": {},
7+
"bookmarks": [
8+
{
9+
"fsPath": "$ROOTPATH$/Algorithms/0842.split-array-into-fibonacci-sequence/split-array-into-fibonacci-sequence_test.go",
10+
"bookmarks": [
11+
38
12+
]
13+
}
14+
]
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,58 @@
11
package problem0842
22

3-
func splitIntoFibonacci(s string) []int {
3+
import (
4+
"strconv"
5+
)
46

7+
func splitIntoFibonacci(s string) []int {
8+
for i := 1; i <= len(s)/2; i++ {
9+
if isLeadingZero(s[:i]) {
10+
break
11+
}
12+
for j := i + 1; max(i, j-i) <= len(s)-j; j++ {
13+
if isLeadingZero(s[i:j]) {
14+
break
15+
}
16+
a, _ := strconv.Atoi(s[:i])
17+
b, _ := strconv.Atoi(s[i:j])
18+
res := make([]int, 2, len(s))
19+
res[0] = a
20+
res[1] = b
21+
if find(a, b, s[j:], &res) {
22+
return res
23+
}
24+
}
25+
}
526
return nil
627
}
28+
29+
func find(a, b int, s string, res *[]int) bool {
30+
if s == "" {
31+
return true
32+
}
33+
34+
c := a + b
35+
if c > 1<<31-1 {
36+
return false
37+
}
38+
cs := strconv.Itoa(c)
39+
csl := len(cs)
40+
41+
if len(s) >= csl && s[:csl] == cs {
42+
*res = append(*res, c)
43+
return find(b, c, s[csl:], res)
44+
}
45+
46+
return false
47+
}
48+
49+
func isLeadingZero(s string) bool {
50+
return len(s) > 1 && s[0] == '0'
51+
}
52+
53+
func max(a, b int) int {
54+
if a > b {
55+
return a
56+
}
57+
return b
58+
}

Algorithms/0842.split-array-into-fibonacci-sequence/split-array-into-fibonacci-sequence_test.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ var tcs = []struct {
1313
ans []int
1414
}{
1515

16+
{
17+
"539834657215398346785398346991079669377161950407626991734534318677529701785098211336528511",
18+
nil,
19+
},
20+
1621
{
1722
"123456579",
1823
[]int{123, 456, 579},
@@ -25,17 +30,17 @@ var tcs = []struct {
2530

2631
{
2732
"112358130",
28-
[]int{},
33+
nil,
2934
},
3035

3136
{
3237
"0123",
33-
[]int{},
38+
nil,
3439
},
3540

3641
{
3742
"1101111",
38-
[]int{110, 1, 111},
43+
[]int{11, 0, 11, 11},
3944
},
4045

4146
// 可以有多个 testcase

0 commit comments

Comments
 (0)