Wednesday, December 2, 2015

Additive Number

Q:
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?
A:

import java.math.BigInteger;

public class Solution {
    public boolean isAdditiveNumber(String num) {
        for(int i =1;i<= num.length()/2;i++){ // i is the end of the index for value a, exclusive
            if(num.charAt(0)=='0' && i != 1)
                break;
            BigInteger a = new BigInteger(num.substring(0,i));
            for(int j = i; j< num.length()-1; j++){ // j is the end of the index for value b, inclusive
                if(num.charAt(i) =='0' && j != i)
                    break;
                BigInteger b = new BigInteger(num.substring(i,j+1));
                if(helper(a, b, num.substring(j+1)))
                    return true;
            }
        }
        return false;
    }
    private boolean helper(BigInteger a,BigInteger b , String str){
        if(str.length()==0)
            return true;
        a = a.add(b);
        String ss = a.toString();
        return str.indexOf(ss)==0 && helper(b,a,str.substring(ss.length()));
    }
}


Mistake:
 第一遍,没有考虑数字溢出的情况。

这里,容易出错的地方,就是i,j的终止条件。

 *********2nd pass************
Using String addition,  instead of using BigInteger

public class Solution {
    public boolean isAdditiveNumber(String num) {
        for(int i =1;i<= num.length() /2;i++){ // for every end i
            for(int j =i+1;j<num.length() ; j++){
                if(isValid(num.substring(0,i),num.substring(i,j),num.substring(j)))
                    return true;
            }
        }
        return false;
    }
    private boolean isValid(String a, String b, String rest){
        if(rest.length() == 0)
            return true;
        if(a.charAt(0)=='0'  && a.length()!=1 )
            return false;
        if(b.charAt(0)=='0'  && b.length()!=1 )
            return false;
        String c = add(a,b, 0);
        return rest.indexOf(c)==0 && isValid(b,c,rest.substring(c.length()));
    }
    
    
    private String add(String a ,String b, int c){
        if(a.length()==0 && b.length()==0)
            return c==0?"":""+c;
        int x = 0, y = 0;
        if( a.length() !=0){
            x = a.charAt(a.length()-1)-'0';
            a = a.substring(0,a.length()-1);
        }
        if( b.length() !=0){
            y = b.charAt(b.length()-1)-'0';
            b = b.substring(0,b.length()-1);
        }        
        int res = (x+y+c)%10;
        c = (x+y+c)/10;
        return add(a, b , c)+res;
    }
}

 the parameter for i, and j both caused error





No comments:

Post a Comment