Additive Number

原创 2015年11月19日 18:07:21

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.

Follow up:

How would you handle overflow for very large input integers?


class Solution {
public:
    bool isAdditiveNumber(string num) {
        int len=num.size();
        for(int i=1;i<=len/2;i++){
            for(int j=1;j<=(len-i)/2;j++){
                string str1=num.substr(0,i);
                string str2=num.substr(i,j);
                if(check2(str1,str2,num.substr(i+j))) return true;
            }
        }
        return false;
    }
    bool check(string str1,string str2,string num){
        string sum=add(str1,str2);
        int n3=sum.size();
        if(sum==num) return true;
        if(n3>=num.size()||sum!=num.substr(0,n3)) return false;
        else return check(str2,sum,num.substr(n3));
    }
    bool check2(string str1,string str2,string num){
        string sum=add(str1,str2);
        while(true){
            if(sum==num) return true;
            if(sum.size()>=num.size()||sum!=num.substr(0,sum.size())) return false;
            num=num.substr(sum.size());
            str1=str2;str2=sum;
            sum=add(str1,str2);
        }
    }
    string add(string str1,string str2){
        int i1=str1.size()-1;
        int i2=str2.size()-1;
        int carry=0;
        string res;
        while(i1>=0||i2>=0){
            int sum=(i1>=0?str1[i1]-'0':0)+(i2>=0?str2[i2]-'0':0)+carry;
            carry=sum/10;
            res.push_back(sum%10+'0');
            i1--;i2--;
        }
        if(carry) res.push_back(carry+'0');
        reverse(res.begin(),res.end());
        return res;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

[leetcode] 306. Additive Number

题目: Additive number is a string whose digits can form additive sequence.A valid additive sequence s...
  • u014673347
  • u014673347
  • 2016年02月21日 11:29
  • 681

[LeetCode306] Additive Number

Additive number is a string whose digits can form additive sequence. A valid additive sequence sh...
  • sbitswc
  • sbitswc
  • 2015年12月06日 03:56
  • 2840

LeetCode306——Additive Number且看我是如何解决的

Additive number is a string whose digits can form additive sequence. A valid additive sequence sh...
  • booirror
  • booirror
  • 2015年12月05日 22:23
  • 1636

LeetCode(306) Additive Number

题目Additive number is a string whose digits can form additive sequence.A valid additive sequence shou...
  • fly_yr
  • fly_yr
  • 2015年12月12日 16:56
  • 1972

LeetCode:306. Additive Number

原题: Additive number is a string whose digits can form additive sequence. A valid additive sequence...
  • x_i_y_u_e
  • x_i_y_u_e
  • 2016年02月23日 16:53
  • 1192

[LeetCode]Additive Number

题目链接:Additive Number题目内容:Additive number is a positive integer whose digits can form additive sequen...
  • xyt8023y
  • xyt8023y
  • 2015年11月23日 14:34
  • 1498

【Leetcode】Additive Number

题目链接:https://leetcode.com/problems/additive-number/ 题目: Additive number is a string whose digits...
  • yeqiuzs
  • yeqiuzs
  • 2016年07月12日 13:49
  • 561

(Java)LeetCode-306. Additive Number

Additive number is a string whose digits can form additive sequence. A valid additive sequence sh...
  • u012848330
  • u012848330
  • 2016年08月08日 20:59
  • 200

[leetode-306]Additive Number(java)

问题描述:这里写链接内容问题分析:这道题比较直观,就是采用回溯法。代码如下:2mspublic class Solution { char[] chars; String num; ...
  • zdavb
  • zdavb
  • 2015年11月22日 18:46
  • 634

Additive number

Additive number is a positive integer whose digits can form additive sequence. A valid additive s...
  • jeason29
  • jeason29
  • 2015年11月18日 20:33
  • 316
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Additive Number
举报原因:
原因补充:

(最多只允许输入30个字)