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

306 Additive Number 加法数的更多相关文章

  1. [LeetCode] Additive Number 加法数

    Additive number is a positive integer whose digits can form additive sequence. A valid additive sequ ...

  2. [LeetCode] 306. Additive Number [Medium]

    306. Additive Number class Solution { private: string stringAddition(string &a, string &b) { ...

  3. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  4. 【刷题-LeetCode】306. Additive Number

    Additive Number Additive number is a string whose digits can form additive sequence. A valid additiv ...

  5. Leetcode 306. Additive Number

    Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...

  6. 306. Additive Number

    题目: Additive number is a string whose digits can form additive sequence. A valid additive sequence s ...

  7. 【LeetCode】306. Additive Number

    题目: Additive number is a string whose digits can form additive sequence. A valid additive sequence s ...

  8. LeetCode(306) Additive Number

    题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...

  9. Additive Number

    Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...

随机推荐

  1. Ubuntu 16.04安装SwitchHosts

    下载: https://github.com/oldj/SwitchHosts/releases 解压: unzip SwitchHosts-linux-x64_v3.3.6.5287.zip 移动: ...

  2. wget下载网络图片

    wget http.......  --no-check-certificate

  3. openstack setup demo 前言

    我们搭建一套三节点的openstanck集群.一个controller节点,两个compute节点.操作系统采用Centos7,操作系统版本信息如下. [root@controller01 ~]# c ...

  4. CSDN 夏令营程序 试题分析 (2)

    题目:若须要在O(nlogn)(以2为底)的时间内完毕对数组的排序.且要求排序是稳定的,则可选择的排序方法是: A.高速排序       B.堆排序            C.归并排序  D.直接插入 ...

  5. 【转】学习JavaScript闭包

    原文: http://www.cnblogs.com/Lau7/p/7942100.html#undefined ------------------------------------------- ...

  6. AutoCAD如何编辑块,打散块

    选中块之后,点击最右侧的最后一个工具"分解"即可.  

  7. firewalld 防火墙 nat 网络地址转换

    目的:实现以下效果 一. 准备环境 @1 三台虚拟机 @2  client 端 ip  192.168.1.2      server端   两块网卡 , ip 分别是 192.168.1.1   和 ...

  8. Windows 7 蓝屏代码大全 &amp; 蓝屏全攻略

    关于Windows 7.Vista等系统的蓝屏.之前软媒在Win7之家和Vista之家都有非常多文章讨论过,可是都是筛选的常见的一些问题,今天这个文章是个大全.希望大家看着别头痛.文章收藏下来以后待查 ...

  9. C++之new和malloc差别

         在C++程序猿面试中.非常easy被问到new 和 malloc的差别.偶尔在quora上逛.看到Robert Love的总结.才发现自己仅仅知道里面的一两项就沾沾自喜,从来没有像这位大牛一 ...

  10. JavaScript变量提升演示样例

    直接先看两段代码 function getSum() { var sum = a + b; var a = 1; var b = 2; return sum; } getSum(); function ...