zoukankan      html  css  js  c++  java
  • 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测试程序源码

  • 相关阅读:
    Labshare 生物信息学在线软件集锦
    为什么要给单个细胞测序?
    两行代码解决Android9.0 CLEARTEXT communication not supported: [ConnectionSpec...
    Android 网络框架:Retrofit2一篇就够了(2020-4-23)
    Android通用流行框架大全
    base64图片裁剪空白区域
    常用的几款抓包工具
    Message: 'chromedriver' executable needs to be in PATH
    nginx+lua+redis做访问鉴权
    win10安装markdownpad2打开显示错误this view has crashed!
  • 原文地址:https://www.cnblogs.com/shine-yr/p/5214719.html
Copyright © 2011-2022 走看看