[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?
题意:
定义Additive数字,最少包含三个数字,除了前两个数字外,其他数字都是满足是前两个数字的和。给一个字符串,里面都是0-9,数字不能以0开始,比如03。现在给一个字符串你,判断下这个字符串是不是Additive数字。
思路:
除了第一个数字和第二个数字可以改变外,后面的数字都是由前两个数字决定的。所以回溯只需要回溯前面两个数字,第一个数字可以是num数组的前1…n/2数字组成的数字,第二个数字可以是从前一个字符串的下一个位置开始到倒数第二个位置。然后把这两个字符串相加得到第三个字符串,查看接下来的子串是不是等于刚才那两个字符串的和。所以我们还需要实现一个字符串的加法(顺便处理了下 大数问题)。
代码如下:

class Solution {
public:
    bool isAdditiveNumber(string num) {
        if(num.length() <= 2)return false;
        return backTracking(num, "", "", 0);
    }
    bool backTracking(string& num, string s1, string s2, int index) {
        if(s1 == "") {
            for(int i = 0; i <= num.length() / 2; i++) {
                auto res = backTracking(num, num.substr(0, i + 1), s2, i + 1);
                if(res)return true;
            }
        }
        else if(s2 == "") {
            for(int i = index; i < num.length() - 1; i++) {
                auto res = backTracking(num, s1, num.substr(index, i - index + 1), i + 1);
                if(res)return true;
            }
        }
        else {
            if(index == num.length())return true;
            auto sum = add(s1, s2);
            if(num[index] == '0' || num.length() - index < sum.length() || sum != num.substr(index, sum.length()))return false;
            return backTracking(num, s2, sum, index + sum.length());
        }
    }
    string add(string s1, string s2) {
        int len1 = s1.length();
        int len2 = s2.length();
        if(len1 == 0)return s2;
        else if(len2 == 0)return s1;
        int carry = 0;
        int length = (len1 > len2)?len1:len2;
        string result(length, '0');
        char c1, c2;
        len1--,len2--,length--;
        for(; len1 >= 0 || len2 >= 0; len1--, len2--, length--) {
            if(len1 < 0)c1 = '0';
            else c1 = s1[len1];
            if(len2 < 0)c2 = '0';
            else c2 = s2[len2];
            int res = c1 + c2 - 2*'0' + carry;
            carry = res/10;
            res = res%10;
            result[length] = '0' + res;
        }
        if(carry == 1)return "1" + result;
        else return result;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值