306. Additive Number

Posted 蜃利的阴影下

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了306. Additive Number相关的知识,希望对你有一定的参考价值。

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 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 containing only digits ‘0‘-‘9‘, write a function to determine if it‘s an additive number.

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

 

 

public class Solution {
    public boolean isAdditiveNumber(String num) {
        int l = num.length();
        if(l<3)
            return false;
        
        if(num.startsWith("00")) //Then the whole string should be "000..."
        {
            for(int i=2;i<l;++i)
            {
                if(num.charAt(i) != ‘0‘)
                    return false;
            }
            return true;
        }
        
        int firstNumEndIndex = (l-1)/2;
        if(num.startsWith("0")) //Then 0 must be the first interger.
            firstNumEndIndex = 1;
        for(int i = 1; i<= firstNumEndIndex; ++i)
        {
            String add1 = num.substring(0, i); //[0, i) => first number
            for(int j = 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 boolean rollToEnd (String num, String add1, String add2, int from)
    {
        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))
            return rollToEnd(num, add2, sumString, from+sumString.length());
        return false;
    }
    
    private String getSumString(String add1, String add2)
    {
        int extra = 0;
        int l1 = add1.length();
        int l2 = add2.length();
        Deque<Integer> sum = new ArrayDeque<Integer>();
        
        while(l1>0 && l2>0)
        {
            int a1 = add1.charAt(l1-1) - ‘0‘;
            int a2 = add2.charAt(l2-1) - ‘0‘;
            int s = a1+a2+extra;
            extra = s/10;
            sum.push(s%10);
            --l1;
            --l2;
        }
        
        while(l1>0)
        {
            int a = add1.charAt(l1-1) - ‘0‘;
            int s = a + extra;
            extra = s/10;
            sum.push(s%10);
            --l1;
        }
        
        while(l2>0)
        {
            int a = add2.charAt(l2-1) - ‘0‘;
            int s = a + extra;
            extra = s/10;
            sum.push(s%10);
            --l2;
        }
        
        if(extra == 1)
            sum.push(extra);
        
        StringBuilder sb = new StringBuilder();
        while(!sum.isEmpty())
            sb.append(sum.pop());
        return sb.toString();
    }
}

 

 

 

 

以上是关于306. Additive Number的主要内容,如果未能解决你的问题,请参考以下文章

刷题-LeetCode306. Additive Number

306.Additive Number

306 Additive Number 加法数

leetcode [306]Additive Number

306. Additive Number

306. Additive Number