Additive number

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.

#include <vector>
#include <iostream>
#include <string>
using namespace std;

class Solution{
public:
        vector<int> additiveNum(int start, int end){
                vector<int> ret;                                                        
                for(int i = start; i <= end; i++)
                        if(isAdditive(i))
                                ret.push_back(i);
                return ret;
        } 
        bool isAdditive(int num){
                string s = to_string(num);
                if(s.length() < 3) return false;
                for(int i = 2; i/2 <= s.length()-i; i++){        // i== length of sum of two first string
                      for(int j = 1; j <= i-1; j++){                        // j== length of first string
                                string s1 = s.substr(0,j);                        // s1: 0,j-1; s2: j,i-1
                                string s2 = s.substr(j,i-j);
                                if(s1[0]=='0' || s2[0]=='0') break;        // 0XX is not allowed
                                int i1 = stoi(s1);
                                int i2 = stoi(s2);
                                int i3 = i1+i2;
                                string s3 = to_string(i3);
                                if(s.find(s3) != i) break;
                                if(helper(s2,s3,s.substr(j))) return true;. more info on 1point3acres.com
                        }
                }
                return false;
        }

        bool helper(string s1, string s2, string s){
                if(s.compare(s1+s2)==0) return true;
                int i1 = stoi(s1); int i2 = stoi(s2);
                int i3 = i1+i2;
                string s3 = to_string(i3);
                int pos = s1.length()+s2.length();
                if(s.find(s3) != pos) return false;
                else return helper(s2,s3,s.substr(s1.length()));. 1point3acres.com/bbs
        }
};

int main(){
        Solution soln;
        vector<int> a = soln.additiveNum(10000,1000000);
        for(int i = 0; i < a.size(); i++). 
                cout<<a[i]<<endl;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值