-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path306_AdditiveNumber.py
executable file
·63 lines (55 loc) · 1.98 KB
/
306_AdditiveNumber.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: [email protected]
class Solution(object):
# According to:
# https://leetcode.com/discuss/70089/python-solution
# The key point is choose first two number then recursively check.
# DFS: recursice implement.
def isAdditiveNumber(self, num):
length = len(num)
for i in range(1, length/2+1):
for j in range(1, (length-i)/2 + 1):
first, second, others = num[:i], num[i:i+j], num[i+j:]
if self.isValid(first, second, others):
return True
return False
def isValid(self, first, second, others):
# Numbers in the additive sequence cannot have leading zeros,
if ((len(first) > 1 and first[0] == "0") or
(len(second) > 1 and second[0] == "0")):
return False
sum_str = str(int(first) + int(second))
if sum_str == others:
return True
elif others.startswith(sum_str):
return self.isValid(second, sum_str, others[len(sum_str):])
else:
return False
class Solution_2(object):
# DFS: iterative implement.
def isAdditiveNumber(self, num):
length = len(num)
for i in range(1, length/2+1):
for j in range(1, (length-i)/2 + 1):
first, second, others = num[:i], num[i:i+j], num[i+j:]
if ((len(first) > 1 and first[0] == "0") or
(len(second) > 1 and second[0] == "0")):
continue
while others:
sum_str = str(int(first) + int(second))
if sum_str == others:
return True
elif others.startswith(sum_str):
first, second, others = (
second, sum_str, others[len(sum_str):])
else:
break
return False
"""
"1123"
"1203"
"112324"
"112334"
"112358"
"""