Additive number

原创 2015年11月18日 20:33:44

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;
}



[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
  • 679

[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
  • 2837

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
  • 1970

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
  • 1634

[LeetCode]Additive Number

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

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
  • 1178

【Leetcode】Additive Number

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

(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
  • 198

[leetode-306]Additive Number(java)

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

Additive Number

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

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