forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
additive-number.py
40 lines (35 loc) · 1.17 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
# 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 xrange(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 xrange(1, len(num)):
for j in xrange(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