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测试程序源码

  • 相关阅读:
    C/C++程序员必备的15个编辑器和集成开发环境
    天猫浏览型应用的CDN静态化架构演变
    实用技巧:如何用 CSS 做到完全垂直居中
    JavaScript 字符串操作:substring, substr, slice
    Hybrid App 开发初探:使用 WebView 装载页面
    引领潮流!最顶尖的24个获奖网页作品
    HTML5编程之旅系列一:HTML5 Geolocation 初探
    JavaScript 秘密花园——对象的使用和属性操作
    提高效率!15款最好的 Bug 跟踪应用程序
    最常用的8款 PHP 调试工具,你用过吗?
  • 原文地址:https://www.cnblogs.com/shine-yr/p/5214719.html
Copyright © 2011-2022 走看看