Additive Number | Leetcode
2015-11-18 19:43
267 查看
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:
also an additive number, the additive sequence is:
Note: Numbers in the additive sequence cannot have leading zeros, so sequence
Given a string represents an integer, write a function to determine if it's an additive number.
Follow up:
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
使用深度搜索,可以很容易解决这道题,代码如下,为了防止越界,需要使用long类型来存储中间值。构建一个辅助函数进行递归判断,传入每次的前两个数,如果后面的数值不论是最小还是最大,都不能等于传入值的和,那么说前一步的取数方式是错误的,需要重新取。
主函数中就是枚举第一次的两个输入。参考代码如下:
{CSDN:CODE:class Solution {
public:
//dfs
bool isAdditiveNumber(string num) {
//return isAdditiveHelper(num,10,1,2147483647,0);
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;
if(isAdditiveHelper(num,j,front,second,0))
return true;
else
break;
}else{
second=second*10+num[j]-'0';
if(isAdditiveHelper(num,j,front,second,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){
return isAdditiveHelper(num,i+step+1,second,result_should,depth+1);
}
++i;
}
return false;
}
};}
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, 03or
1, 02, 3is invalid.
Given a string represents an integer, write a function to determine if it's an additive number.
Follow up:
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
使用深度搜索,可以很容易解决这道题,代码如下,为了防止越界,需要使用long类型来存储中间值。构建一个辅助函数进行递归判断,传入每次的前两个数,如果后面的数值不论是最小还是最大,都不能等于传入值的和,那么说前一步的取数方式是错误的,需要重新取。
主函数中就是枚举第一次的两个输入。参考代码如下:
{CSDN:CODE:class Solution {
public:
//dfs
bool isAdditiveNumber(string num) {
//return isAdditiveHelper(num,10,1,2147483647,0);
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;
if(isAdditiveHelper(num,j,front,second,0))
return true;
else
break;
}else{
second=second*10+num[j]-'0';
if(isAdditiveHelper(num,j,front,second,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){
return isAdditiveHelper(num,i+step+1,second,result_should,depth+1);
}
++i;
}
return false;
}
};}
相关文章推荐
- 信号处理国际会议排名
- Storm常见模式1——BasicBolt
- POJ 1664 放苹果
- 数据库 select @@identity说明
- [TwistedFate]UIScrollView UIPageControl
- Android-接口回调
- 对于人民币符号的总结
- 【Leetcode-easy】Palindrome Number
- 一般互联网公司通用的c++代码规范
- PUOJ_Stars
- 二级指针与二维数组
- 空间商关闭fsockopen和pfsockopen函数导致phpcms 无法发送邮件的解决办法
- 【ZJOI2009】狼和羊的故事
- ListView嵌套GridView显示不全解决方法
- 查找算法总结
- mysql设置远程登录后连接不上
- webservice第一天
- Android性能测试之概述(一)
- Python中指针式传递的函数遇到的问题
- NodeJs 安全