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.

分析

给定一个字符串,题目要求判断该字符串是否为约束规则下的可加字符串。

手动判断很容易,但是转为程序实现开始不知从何入手。

查阅一些资料,最终搞定。详见代码。

AC代码

class Solution {
public:
    bool isAdditiveNumber(string num) {
        if (num.empty())
            return false;

        int len = num.size();
        for (int i = 1; i < len - 1; ++i)
        {
            string a = num.substr(0, i);

            //非首个以0开头的加数违反规则
            if (a[0] == '0' && i > 1)
                continue;

            for (int j = 1; j < len; ++j)
            {
                string b = num.substr(i, j);
                if (b[0] == 0 && j > 1)
                    continue;
                string ret = add(a, b);
                if (i + j + ret.length() > len)
                    continue;

                //存储原字符串中和上一和 同样长度的子串
                string val = num.substr(i + j, ret.length());

                //当前已经相加的末尾下标
                int pass = i + j;

                string tmp;
                while (ret == val)
                {
                    //判断是否到字符串末尾
                    pass += val.length();
                    if (len == pass)
                        return true;
                    tmp = b;
                    b = ret;
                    //下一步骤加法实现
                    ret = add(tmp, b);
                    val = num.substr(pass, ret.length());
                }//while
            }//for
        }//for
        return false;
    }

    //字符串加法实现
    string add(string a, string b)
    {
        int len_a = a.size(), len_b = b.size();

        string ret = "";
        int i = len_a - 1, j = len_b - 1, carry = 0;

        while (i>=0 && j>=0)
        {
            int tmp = a[i] + b[j] - 2 * '0' + carry;
            carry = tmp / 10;
            char c = tmp % 10 + '0';

            ret += c;
            --i;
            --j;
        }//while

        while (i >= 0)
        {
            int tmp = a[i] - '0' + carry;
            carry = tmp / 10;
            char c = tmp % 10 + '0';

            ret += c;
            --i;
        }//while

        while (j >= 0)
        {
            int tmp = b[j] - '0' + carry;
            carry = tmp / 10;
            char c = tmp % 10 + '0';

            ret += c;
            --j;
        }//while

        if (carry != 0)
            ret += carry + '0';

        reverse(ret.begin(), ret.end());

        return ret;
    }
};

GitHub测试程序源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值