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

Commit d503f4c

Browse files
aQuaaQua
aQua
authored and
aQua
committed
842 finish
1 parent 2e3991e commit d503f4c

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

.vscode/bookmarks.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{
99
"fsPath": "$ROOTPATH$/Algorithms/0842.split-array-into-fibonacci-sequence/split-array-into-fibonacci-sequence_test.go",
1010
"bookmarks": [
11-
38
11+
43
1212
]
1313
}
1414
]

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

+15-15
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ func splitIntoFibonacci(s string) []int {
1515
}
1616
a, _ := strconv.Atoi(s[:i])
1717
b, _ := strconv.Atoi(s[i:j])
18-
res := make([]int, 2, len(s))
19-
res[0] = a
20-
res[1] = b
18+
res := make([]int, 0, len(s))
19+
res = append(res, a, b)
2120
if find(a, b, s[j:], &res) {
2221
return res
2322
}
@@ -27,23 +26,24 @@ func splitIntoFibonacci(s string) []int {
2726
}
2827

2928
func find(a, b int, s string, res *[]int) bool {
30-
if s == "" {
31-
return true
32-
}
29+
for len(s) > 0 {
30+
c := a + b
31+
if c > 1<<31-1 {
32+
return false
33+
}
34+
cs := strconv.Itoa(c)
35+
csl := len(cs)
3336

34-
c := a + b
35-
if c > 1<<31-1 {
36-
return false
37-
}
38-
cs := strconv.Itoa(c)
39-
csl := len(cs)
37+
if len(s) < csl || s[:csl] != cs {
38+
return false
39+
}
4040

41-
if len(s) >= csl && s[:csl] == cs {
4241
*res = append(*res, c)
43-
return find(b, c, s[csl:], res)
42+
a, b = b, c
43+
s = s[csl:]
4444
}
4545

46-
return false
46+
return true
4747
}
4848

4949
func isLeadingZero(s string) bool {

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,20 @@ var tcs = []struct {
1414
}{
1515

1616
{
17-
"539834657215398346785398346991079669377161950407626991734534318677529701785098211336528511",
18-
nil,
17+
"1320581321313221264343965566089105744171833277577",
18+
[]int{13205, 8, 13213, 13221, 26434, 39655, 66089, 105744, 171833, 277577},
1919
},
2020

2121
{
2222
"123456579",
2323
[]int{123, 456, 579},
2424
},
2525

26+
{
27+
"539834657215398346785398346991079669377161950407626991734534318677529701785098211336528511",
28+
nil,
29+
},
30+
2631
{
2732
"11235813",
2833
[]int{1, 1, 2, 3, 5, 8, 13},

0 commit comments

Comments
 (0)