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

  • 相关阅读:
    hive函数之~字符串函数
    hive函数之~条件函数
    JSONP使用及注意事项小结
    css命名管理混乱?不妨试试BEM
    【移动端debug-6】如何做一个App里的web调试小工具
    ES6学习笔记(五):Class和Module
    ES6学习笔记(三):与迭代相关的新东东
    ES6学习笔记(四):异步操作
    ES6学习笔记(二):引用数据类型
    CORS跨域资源共享简述
  • 原文地址:https://www.cnblogs.com/shine-yr/p/5214719.html
Copyright © 2011-2022 走看看