-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path306. Additive Number.py
More file actions
31 lines (27 loc) · 970 Bytes
/
306. Additive Number.py
File metadata and controls
31 lines (27 loc) · 970 Bytes
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
class Solution:
def isAdditiveNumber(self, num: str) -> bool:
length=len(num)
def isactive(num1,num2,k):
num3=num1+num2
index=k+1
while index<=length:
if str(int(num[k:index]))==num[k:index] and int(num[k:index])==num3:
num1=num2
num2=num3
num3=num1+num2
return index==length or isactive(num1, num2, index)
else:
index+=1
return False
for i in range(1,length//2+1):
for j in range(1,length//2+1):
num1=int(num[:i])
if str(int(num[i:i+j]))!=num[i:i+j]:
continue
num2=int(num[i:i+j])
if isactive(num1,num2, i+j):
return True
return False
if __name__ == "__main__":
x=Solution()
print(x.isAdditiveNumber("1203"))