-
Notifications
You must be signed in to change notification settings - Fork 43
/
additive-number.py
95 lines (87 loc) · 2.7 KB
/
additive-number.py
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
# V0
# V1
# http://bookshadow.com/weblog/2015/11/18/leetcode-additive-number/
# IDEA : ITERATION
class Solution(object):
def isAdditiveNumber(self, num):
"""
:type num: str
:rtype: bool
"""
n = len(num)
for i, j in itertools.combinations(range(1, n), 2):
a, b = num[:i], num[i:j]
if a != str(int(a)) or b != str(int(b)):
continue
while j < n:
c = str(int(a) + int(b))
if not num.startswith(c, j):
break
j += len(c)
a, b = b, c
if j == n:
return True
return False
# V1'
# http://bookshadow.com/weblog/2015/11/18/leetcode-additive-number/
# IDEA : RECURSION
class Solution(object):
def isAdditiveNumber(self, num):
"""
:type num: str
:rtype: bool
"""
def isValid(num):
return len(num) == 1 or num[0] != '0'
def search(a, b, c):
d = str(int(a) + int(b))
if not isValid(d) or not c.startswith(d):
return False
if c == d:
return True
return search(b, d, c[len(d):])
size = len(num)
for x in range(1, size / 2 + 1):
for y in range(x + 1, size):
a, b, c = num[:x], num[x:y], num[y:]
if not isValid(a) or not isValid(b):
continue
if search(a, b, c):
return True
return False
# V2
# Time: O(n^3)
# Space: O(n)
class Solution(object):
def isAdditiveNumber(self, num):
"""
:type num: str
:rtype: bool
"""
def add(a, b):
res, carry, val = "", 0, 0
for i in range(max(len(a), len(b))):
val = carry
if i < len(a):
val += int(a[-(i + 1)])
if i < len(b):
val += int(b[-(i + 1)])
carry, val = val / 10, val % 10
res += str(val)
if carry:
res += str(carry)
return res[::-1]
for i in range(1, len(num)):
for j in range(i + 1, len(num)):
s1, s2 = num[0:i], num[i:j]
if (len(s1) > 1 and s1[0] == '0') or \
(len(s2) > 1 and s2[0] == '0'):
continue
expected = add(s1, s2)
cur = s1 + s2 + expected
while len(cur) < len(num):
s1, s2, expected = s2, expected, add(s2, expected)
cur += expected
if cur == num:
return True
return False