博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
306 Additive Number 加法数
阅读量:4705 次
发布时间:2019-06-10

本文共 1331 字,大约阅读时间需要 4 分钟。

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
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()

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

转载于:https://www.cnblogs.com/xidian2014/p/8831036.html

你可能感兴趣的文章
python作业
查看>>
Execl DataTime Format Number
查看>>
将代码上传版本库gitee
查看>>
表单多次提交
查看>>
无锁版以时间为GUID的方法
查看>>
ubuntu12.10安装source navigator
查看>>
算法题解
查看>>
linux服务器安全小知识
查看>>
[信息安全] 1.密码工具箱
查看>>
Linux下基于源代码方式安装MySQL 5.6
查看>>
Jexus & Mono 迁移
查看>>
JSP HTML 各种 乱码 解决方法|jsp include html乱码|include 乱码|MyEclipse 中文乱码
查看>>
JavaScript (一)
查看>>
使用Iterator的hasNext方法,next方法遍历集合
查看>>
解决ESET/NOD32/ESS下IIS不能访问
查看>>
《把时间当作朋友》1
查看>>
接口测试及服务器性能压测
查看>>
ES6 我的总结学习
查看>>
Tool
查看>>
src与href的区别。
查看>>