306. Additive Number
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 32% Topics: String, Backtracking
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# 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
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions