306. Additive Number

Source: Internet
Author: User

Additive number is a string whose digits can form Additive sequence.

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

For example:
"112358"is a 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 a additive number, the additive sequence is: 1, 99, 100, 199.
1 + 99 = 100, 99 + 100 = 199

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

Given a string containing only digits ‘0‘-‘9‘ , write a function to determine if it's an additive number.

Follow up:
How would handle overflow for very large input integers?

 Public classSolution { Public Booleanisadditivenumber (String num) {intL =num.length (); if(l<3)            return false; if(Num.startswith ("00"))//Then the whole string should is "..."        {             for(inti=2;i<l;++i) {if(Num.charat (i)! = ' 0 ')                    return false; }            return true; }                intFirstnumendindex = (L-1)/2; if(Num.startswith ("0"))//Then 0 must is the first Interger.Firstnumendindex = 1;  for(inti = 1; I<= Firstnumendindex; ++i) {String add1= num.substring (0, I);//[0, i) + = First number             for(intj = i+1; j<= L-math.max (i,j-i); ++j) {String add2= Num.substring (i, j);//[I, j] = = Second number                if(Add2.equals ("0"))                {                    if(Rolltoend (num, Add1, ADD2, j))return true;  Break; }                    if(Rolltoend (num, Add1, ADD2, j))return true; }        }        return false; }            //[Start,end]    Private BooleanRolltoend (String num, String add1, String add2,intFrom ) {String sumstring=getsumstring (ADD1, ADD2); String Rest=num.substring (from); if(Rest.startswith ("0"))            return false; if(Rest.equals (sumstring))return true; if(Rest.startswith (sumstring))returnRolltoend (num, add2, sumstring, from+sumstring.length ()); return false; }        Privatestring getsumstring (String add1, String add2) {intExtra = 0; intL1 =add1.length (); intL2 =add2.length (); Deque<Integer> sum =NewArraydeque<integer>();  while(L1>0 && l2>0)        {            intA1 = Add1.charat (l1-1)-' 0 '; intA2 = Add2.charat (l2-1)-' 0 '; ints = a1+a2+Extra; Extra= S/10; Sum.push (S%10); --L1; --L2; }                 while(l1>0)        {            intA = Add1.charat (l1-1)-' 0 '; ints = A +Extra; Extra= S/10; Sum.push (S%10); --L1; }                 while(l2>0)        {            intA = Add2.charat (l2-1)-' 0 '; ints = A +Extra; Extra= S/10; Sum.push (S%10); --L2; }                if(Extra = = 1) Sum.push (extra); StringBuilder SB=NewStringBuilder ();  while(!sum.isempty ()) Sb.append (Sum.pop ()); returnsb.tostring (); }}

306. Additive number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.