给定一个数字字符串 S
,比如 S = "123456579"
,我们可以将它分成斐波那契式的序列 [123, 456, 579]
。
形式上,斐波那契式序列是一个非负整数列表 F
,且满足:
0 <= F[i] <= 2^31 - 1
,(也就是说,每个整数都符合 32 位有符号整数类型);F.length >= 3
;- 对于所有的
0 <= i < F.length - 2
,都有F[i] + F[i+1] = F[i+2]
成立。
另外,请注意,将字符串拆分成小块时,每个块的数字一定不要以零开头,除非这个块是数字 0 本身。
返回从 S
拆分出来的所有斐波那契式的序列块,如果不能拆分则返回 []
。
示例 1:
输入:"123456579" 输出:[123,456,579]
示例 2:
输入: "11235813" 输出: [1,1,2,3,5,8,13]
示例 3:
输入: "112358130" 输出: [] 解释: 这项任务无法完成。
示例 4:
输入:"0123" 输出:[] 解释:每个块的数字不能以零开头,因此 "01","2","3" 不是有效答案。
示例 5:
输入: "1101111" 输出: [110, 1, 111] 解释: 输出 [11,0,11,11] 也同样被接受。
提示:
1 <= S.length <= 200
字符串 S
中只含有数字。
C++
class Solution {
public:
bool judge(int i, int start, string S, vector<string> tmp)
{
if(i-start+1>1 && '0'==S[start])
{
return false;
}
if(i-start+1<=10)
{
if(tmp.size()<=1)
{
return true;
}
else
{
int n=tmp.size();
long long a=atoi(tmp[n-1].c_str());
long long b=atoi(tmp[n-2].c_str());
long long c=atoi(S.substr(start,i-start+1).c_str());
if(a+b==c)
{
return true;
}
else
{
return false;
}
}
}
return false;
}
void dfs(string S, int start, vector<string>& tmp, vector<vector<string>>& vec)
{
if(start==S.length())
{
if(tmp.size()>=3)
{
vec.push_back(tmp);
}
return;
}
for(int i=start;i<S.length();i++)
{
if(judge(i,start,S,tmp))
{
tmp.push_back(S.substr(start,i-start+1));
dfs(S,i+1,tmp,vec);
tmp.pop_back();
}
}
}
vector<int> splitIntoFibonacci(string S)
{
vector<string> tmp;
vector<vector<string>> vec;
dfs(S,0,tmp,vec);
if(vec.size())
{
auto res=vec[0];
vector<int> ans;
for(auto it:res)
{
ans.push_back(atoi(it.c_str()));
}
return ans;
}
return {};
}
};