Additive Number(306 Medium)加法数

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?

题目大意:

累加数是指其各位数字可以组成累加序列的正数。

一个有效的累加序列应该包含至少3个数字。除前两个数以外,序列中剩下的数字都应该迭代地等于前面两个数的和(类似于斐波那契数列)。

枚举前两个数,然后循环判断剩余的数是否满足累加序列。

解法1:Backtracking with Pruning

public boolean isAdditiveNumber(String num) {
        if (num == null || num.length() < 3) return false;
        int n = num.length();

        for (int i = 1; i < n; i++) {
            if (i > 1 && num.charAt(0) == '0')
                break;

            for (int j = i+1; j < n; j++) {
                int first = 0, second = i, third = j;
                if (num.charAt(second) == '0' && third > second+1)
                    break;

                while (third < n) {
                    Long result = (Long.parseLong(num.substring(first, second)) +
                            Long.parseLong(num.substring(second, third)) );

                    if (num.substring(third).startsWith(result.toString())) {
                        first = second;
                        second = third;
                        third += result.toString().length();
                    } else {
                        break;
                    }
                }

                if (third == n)
                    return true;
            }//j
        }//i

        return false;
}

解法2:another solution

Note that the length of first two numbers can't be longer than half of the initial string, so the two loops in the first function will end when i>num.size()/2 and j>(num.size()-i)/2, this will actually save a lot of time.

Update the case of heading 0s e.g. "100010" should return false

 private boolean check(String a, String b, String c) {
    if (a.length() > 1 && a.charAt(0) == '0' || b.length() > 1 && b.charAt(0) == '0')
        return false;

    //String sum = add(a, b);
    Long sumLong = Long.parseLong(a) + Long.parseLong(b);
    String sum =sumLong.toString();
    if (sum.equals(c))
        return true;

    if (c.length() <= sum.length() || !c.startsWith(sum))
        return false;

    return check(b, sum, c.substring(sum.length()));
}

public boolean isAdditiveNumber(String num) {
        if(num == null)
            return false;
        num = num.trim();
        if(num.length() < 3)
            return false;

    for (int i = 1; i <= (num.length() >> 1); i++) {
        for (int j = 1; j <= ((num.length() - i) >> 1); j++) {
            if (check(num.substring(0, i),num.substring(i,i + j), num.substring(i + j)))
                return true;
        }
    }
    return false;
}

results matching ""

    No results matching ""