码迷,mamicode.com
首页 > 其他好文 > 详细

306. Additive Number

时间:2016-06-19 06:47:40      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/neweracoding/p/5597349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!