You are given a string of digits num
, such as "123456579"
. We can split it into a Fibonacci-like sequence [123, 456, 579]
.
Formally, a Fibonacci-like sequence is a list f
of non-negative integers such that:
0 <= f[i] < 231
, (that is, each integer fits in a 32-bit signed integer type),f.length >= 3
, andf[i] + f[i + 1] == f[i + 2]
for all0 <= i < f.length - 2
.
Note that when splitting the string into pieces, each piece must not have extra leading zeroes, except if the piece is the number 0
itself.
Return any Fibonacci-like sequence split from num
, or return []
if it cannot be done.
Input: num = "1101111" Output: [11,0,11,11] Explanation: The output [110, 1, 111] would also be accepted.
Input: num = "112358130" Output: [] Explanation: The task is impossible.
Input: num = "0123" Output: [] Explanation: Leading zeroes are not allowed, so "01", "2", "3" is not valid.
1 <= num.length <= 200
num
contains only digits.
class Solution:
def splitIntoFibonacci(self, num: str) -> List[int]:
for i in range(1, min(11, (len(num) + 1) // 2)):
if i > 1 and num[0] == '0':
break
for j in range(i + 1, min(i + 11, len(num))):
if j - i > 1 and num[i] == '0':
break
seq = [int(num[:i]), int(num[i:j])]
k = j
while k < len(num):
size = len(str(seq[-2] + seq[-1]))
seq.append(int(num[k:k + size]))
k += size
if seq[-1] >= 2147483648 or seq[-3] + seq[-2] != seq[-1]:
seq = []
break
if seq != []:
return seq
return []