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. response的作用

    response.addCookies(),添加Cookie. response.sendRedirect()页面跳转,客户端跳转.(能够取到request)

  2. springboot+idea+maven学习第一天(springboot入门,idea整合maven)

    1.springboot简介 简化Spring 用用开发的一个框架: 真个S僻壤技术栈的一个大整合 j2ee开发的一站式解决方案 2.微服务 微服务:是一种架构风格 一个应用应该是一组小型服务:可以通 ...

  3. USB2.0的最高传输速率

    USB2.0除了拥有USB1.1中规定的1.5Mbps和12Mbps两个传输模式以外,还增加了480Mbps高速数据传输模式(注:第二版USB2.0的传输速率将达800Mbps,最高理想值1600Mb ...

  4. mybatis xml标签,批量插入

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...

  5. Linux学习系列之LNMP

    LNMP介绍 LNMP是什么 LNMP(Linux-Nginx-MySQL-PHP)网站架构是目前国际流行的Web架构; 这四种软件组合,可以成为一个免费.高效.扩展性强的Web架构; LNMP原理图 ...

  6. JavaSE入门学习5:Java基础语法之keyword,标识符,凝视,常量和变量

    一keyword keyword概述:Java语言中有一些具有特殊用途的词被称为keyword.keyword对Java的编译器有着特殊的意义.在程 序中应用时一定要谨慎. keyword特点:组成k ...

  7. 【Linux多线程】三个经典同步问题

    在了解了<同步与互斥的区别>之后,我们来看看几个经典的线程同步的例子.相信通过具体场景可以让我们学会分析和解决这类线程同步的问题,以便以后应用在实际的项目中. 一.生产者-消费者问题 问题 ...

  8. 【Linux多线程】同步与互斥的区别

    同步与互斥这两个概念经常被混淆,所以在这里说一下它们的区别. 一.同步与互斥的区别 1. 同步 同步,又称直接制约关系,是指多个线程(或进程)为了合作完成任务,必须严格按照规定的 某种先后次序来运行. ...

  9. hadoop分布式安装部署具体视频教程(网盘附配好环境的CentOS虚拟机文件/hadoop配置文件)

    參考资源下载:http://pan.baidu.com/s/1ntwUij3视频安装教程:hadoop安装.flvVirtualBox虚拟机:hadoop.part1-part5.rarhadoop文 ...

  10. [译]IOS中AutoLayout布局与Transform的冲突问题

    http://m.blog.csdn.net/blog/a345017062/43565279 原文链接见这里: http://stackoverflow.com/questions/12943107 ...