Fork me on GitHub

【LeetCode】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?

提示:

这题在题目中已经给出了一个重要的提示揭示了题目中的一个小陷阱,由于输入是字符串,所以我们在做加法的时候可能会溢出,所以需要实现一个基于字符串的加法,比如像下面这样:

string add(string a, string b) {
    int i = a.length() - 1, j = b.length() - 1, carry = 0;
    string res;
    while (i >= 0 || j >= 0) {
        int digit = carry + (i >= 0 ? a[i--] - '0' : 0) + (j >= 0 ? b[j--] - '0' : 0);
        res.push_back(digit % 10 + '0');
        carry = digit / 10;
    }
    if (carry) {
        res.push_back(carry + '0');
    }
    reverse(res.begin(), res.end());
    return res;
}

然后就是要想出算法的整体解决思路,我的思路就是递归求解,一旦发现一个成功的Additive Number就返回true。这里循环内的终止条件就尤为重要了,若可以切割掉一些显然不成立的分支,那么算法的速度就能得到提升。这里的思路是这样的:

  • 由于给定的字符串需要划分成至少三个数字,所以第一个数字的长度最多等于字符串长度的一半,因为整除是向下取整,所以取等号:
  • for (int i = 1; i <= num.size() / 2; ++i)
  • 因为是求和,所以第二个字符串的长度只能最多为剩下字符串长度的一半:
  • for (int j = 1; j  <= (num.size()-i)/2; ++j)

     

 最后就是递归函数中的终止条件了,这些都比较简单,详见代码。

代码:

class Solution {
public:
    bool isAdditiveNumber(string num) {
        for (int i = 1; i <= num.size() / 2; ++i) {
            for (int j = 1; j  <= (num.size()-i)/2; ++j) {
                if (check(num.substr(0, i), num.substr(i, j), num.substr(i+j))) {
                    return true;
                }
            }
        }
        return false;
    }
    
    bool check(string a, string b, string c) {
        if (a.size() > 1 && a[0] == '0' || b.size() > 1 && b[0] == '0') {
            return false;
        }
        string r = add(a, b);
        if (r == c) {
            return true;
        }
        if (r.size() >= c.size()) {
            return false;
        }
        for (int i = 0; i < r.size(); ++i) {
            if (r[i] != c[i]) {
                return false;
            }
        }
        return check(b, r, c.substr(r.size()));
    }
    
    string add(string a, string b) {
        int i = a.length() - 1, j = b.length() - 1, carry = 0;
        string res;
        while (i >= 0 || j >= 0) {
            int digit = carry + (i >= 0 ? a[i--] - '0' : 0) + (j >= 0 ? b[j--] - '0' : 0);
            res.push_back(digit % 10 + '0');
            carry = digit / 10;
        }
        if (carry) {
            res.push_back(carry + '0');
        }
        reverse(res.begin(), res.end());
        return res;
    }
};
posted @ 2016-03-21 14:22  __Neo  阅读(532)  评论(0编辑  收藏  举报