forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
additive-number.go
executable file
·64 lines (55 loc) · 1.18 KB
/
additive-number.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 problem0306
import (
"strconv"
)
func isAdditiveNumber(num string) bool {
if len(num) < 3 {
return false
}
var dfs func(int, int, string) bool
dfs = func(n1, n2 int, num string) bool {
n3 := n1 + n2
s3 := strconv.Itoa(n3)
if len(s3) > len(num) {
return false
}
if len(s3) == len(num) {
if s3 == num {
return true
}
return false
}
if s3 != num[:len(s3)] {
return false
}
return dfs(n2, n3, num[len(s3):])
}
// n1 = num[:i]
// n2 = num[i:j]
// n3 = num[j:j+x], x = 1,2,3..
var i, j int
// Jmax 是 j 的最大值
// n1, n2 中较大数的 size 的最小值为 (j+1)/2
// n3 的 size 的最大值为 len(num)-j
// 当 (j+1)/2 <= len(num)-j 才有可能使得 n1+n2==n3 成立
// 可得 j <= (2* len(num)-1)/3 < len(num) * 2 / 3
// 考虑到 int 除法的特性
// Jmax = len(num) * 2 / 3
Jmax := len(num) * 2 / 3
for i = 1; i <= Jmax-1; i++ {
if len(num[:i]) > 1 && num[0] == '0' {
return false
}
for j = i + 1; j <= Jmax; j++ {
if len(num[i:j]) > 1 && num[i] == '0' {
break
}
n1, _ := strconv.Atoi(num[:i])
n2, _ := strconv.Atoi(num[i:j])
if dfs(n1, n2, num[j:]) {
return true
}
}
}
return false
}