Additive number is a positive integer whose digits can form additive sequence.

A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.

For example:
"112358" is an additive number because the digits can form an additive sequence: 1, 1, 2, 3, 5, 8.

1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
"199100199" is also an additive number, the additive sequence is: 1, 99, 100, 199.
1 + 99 = 100, 99 + 100 = 199

Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.

Given a string represents an integer, write a function to determine if it's an additive number.

How would you handle overflow for very large input integers?

Credits:
Special thanks to @jeantimex for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

{CSDN:CODE:class Solution {
public:
//dfs
if(num.length()<3)
return false;
int step=0;
//防止越界
long front=0,second=0;
for(int i=0;i<num.length()-1;){
front=front*10+num[i]-'0';
int j=i+1;
second=0;
for(;j<num.length();){
if(j==i+1&&num[j]=='0'){
second=0;
return true;
else
break;
}else{
second=second*10+num[j]-'0';
return true;
++j;
}
}
++i;
}
return false;
}

bool isAdditiveHelper(string num,int step,long front,long second,int depth){
if(depth==0&&step==num.length()-1)
return false;
else if(depth>0&&step==num.length()-1){
return true;
}
string temp=num.substr(step+1,num.length()-step-1);
long result_should=front+second;
long res_get=0;
int i=0;
for(;i<temp.length();){
if(i==0&&temp[i]=='0'){
return false;
}
res_get=res_get*10+temp[i]-'0';
if(res_get>result_should)
return false;
else if(res_get==result_should){
}
++i;
}
return false;
}
};}

• 本文已收录于以下专栏：

• sbitswc
• 2015年12月06日 03:56
• 2840

• booirror
• 2015年12月05日 22:23
• 1636

• yeqiuzs
• 2016年07月12日 13:49
• 562

• u014673347
• 2016年02月21日 11:29
• 682

• fly_yr
• 2015年12月12日 16:56
• 1972

• x_i_y_u_e
• 2016年02月23日 16:53
• 1193

• xyt8023y
• 2015年11月23日 14:34
• 1498

• youthinkwu
• 2016年03月16日 17:56
• 220

• niuooniuoo
• 2016年05月18日 11:03
• 390