• 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.

    Given a string containing only digits '0'-'9', write a function to determine if it's an additive number.

    Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.

    Example 1:

    Input: "112358"
    Output: true 
    Explanation: 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
    

    Example 2:

    Input: "199100199"
    Output: true 
    Explanation: The additive sequence is: 1, 99, 100, 190.
                 1 + 99 = 100, 99 + 100 = 199

    Approach #1: 

    class Solution {
    public:
        bool isAdditiveNumber(string num) {
            if (num.length() < 3) return false;
            // if (num == "000") return true;
            // cout <<"num.length() = " << num.length() << endl;
            for (int i = 1; i <= num.length() / 2; ++i) {    // the first number's length;
                if (num[0] == '0' && i > 1) return false;
                string s_first = num.substr(0, i);
                long first = stol(s_first);
                // cout << "first = " << s_first << endl;
                for (int j = 1; num.length()-i-j >= max(i, j); ++j) {
                    string s_second = num.substr(i, j);
                    if (j > 1 && s_second[0] == '0') break;
                    long second = stol(s_second);
                    // cout << "second = " << s_second << endl;
                    if (isValid(first, second, i+j, num)) {
                        return true;
                    }
                }
            }
            return false;
        }
        
    private:
        bool isValid(long first, long second, int start, string num) {
            // cout << "start = " << start << endl;
            // cout << first << ' ' << second << endl;
            if (start == num.length()) return true;
            second = second + first;
            first = second - first;
            // cout << first << ' ' << second << endl;
            string sum = to_string(second);
            // cout << "sum = " << sum << endl;
            return C_startWith(sum, start, num) && isValid(first, second, start+sum.length(), num);
        }
        
        bool C_startWith(string sum, int start, string num) {
            int len = sum.length();
            string temp = num.substr(start, len);
            return temp == sum;
        }
    };
    

      

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    招隐-古琴曲-山中鸣琴,万籁声沉沉,何泠泠!
    因循苟且逸豫而无为,可以侥幸一时,而不可以旷日持久。——王安石
    模糊理论在图像处理中的应用
    铁关-中国首都警官合唱团-歌词
    听着总感觉莫名熟悉的音乐汇总
    石鼓歌-韩愈
    唐长安城
    唐长安的信仰——读书笔记
    Eclipse安装java web插件
    Java调用MySql数据库函数
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10357028.html
Copyright © 2020-2023  润新知