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.
Follow up:
How would you handle overflow for very large input integers?

详见:https://leetcode.com/problems/additive-number/description/

C++:

class Solution {
public:
    bool isAdditiveNumber(string num) {
        for(int i=1;i<num.size();++i)
        {
            for(int j=i+1;j<num.size();++j)
            {
                string s1=num.substr(0,i);
                string s2=num.substr(i,j-i);
                long long d1=atoll(s1.c_str()),d2=atoll(s2.c_str());
                if((s1.size()>1&&s1[0]==‘0‘)||(s2.size()>1&&s2[0]==‘0‘))
                {
                    continue;
                }
                long long next=d1+d2;
                string nexts=to_string(next);
                string now=s1+s2+nexts;
                while(now.size()<num.size())
                {
                    d1=d2;
                    d2=next;
                    next=d1+d2;
                    nexts=to_string(next);
                    now+=nexts;
                }
                if(now==num)
                {
                    return true;
                }
            }
        }
        return false;
    }
};

参考:https://www.cnblogs.com/grandyang/p/4974115.html

原文地址:https://www.cnblogs.com/xidian2014/p/8831036.html

时间: 2024-10-16 03:11:38

306 Additive Number 加法数的相关文章

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 e

[leetcode] 306. Additive Number 解题报告

题目链接: https://leetcode.com/problems/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

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 e

306. Additive Number java solutions

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 e

Additive Number 306

题目链接:https://leetcode.com/problems/additive-number/ 题目描述:一串数字是Additive number,必须存在一个划分将这一串数字分成n个数字,n必须大于3,并且除了前两个数字之外,每个数字都是前两个数字之和,此外,不存在包含前导零的数字. 题目分析:深度搜索 代码实现: #include <map> #include <vector> #include <algorithm> #include <cstrin

[LeetCode][JavaScript]Additive Number

Additive Number Additive number is a positive integer 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 o

Leetcode: 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 e

Additive Number

没啥好说的,注意用long(明明题目里说integer,汗) public class Solution { public boolean isAdditiveNumber(String num) { int length = num.length(); int i = 1; while (i < length && Long.valueOf(num.substring(0, i)) <= Integer.MAX_VALUE) { int j = i + 1; while (j

leetcode刷题记录(2)

301. Remove Invalid Parentheses Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples: "()())()&q