-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy path306.py
More file actions
20 lines (20 loc) · 775 Bytes
/
306.py
File metadata and controls
20 lines (20 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
def isAdditiveNumber(self, num):
def getStarter():
arr = []
for i in range(1, len(num) - 1):
for j in range(i + 1, len(num)):
s1, s2 = num[:i], num[i:j]
if (s1[0] == "0" and len(s1) > 1) or (s2[0] == "0" and len(s2) > 1):
continue
arr.append((int(s1), int(s2), j))
return arr
def dfs(pre1, pre2, i):
if i == len(num):
return True
sm = pre1 + pre2
l = len(str(sm))
new = int(num[i:i + l])
return new == sm and dfs(pre2, new, i + l)
q = getStarter()
return any(dfs(p1, p2, i) for p1, p2, i in q)