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
306 Additive Number 加法数的更多相关文章
- [LeetCode] Additive Number 加法数
Additive number is a positive integer whose digits can form additive sequence. A valid additive sequ ...
- [LeetCode] 306. Additive Number [Medium]
306. Additive Number class Solution { private: string stringAddition(string &a, string &b) { ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【刷题-LeetCode】306. Additive Number
Additive Number Additive number is a string whose digits can form additive sequence. A valid additiv ...
- Leetcode 306. Additive Number
Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...
- 306. Additive Number
题目: Additive number is a string whose digits can form additive sequence. A valid additive sequence s ...
- 【LeetCode】306. Additive Number
题目: Additive number is a string whose digits can form additive sequence. A valid additive sequence s ...
- LeetCode(306) Additive Number
题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...
- Additive Number
Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...
随机推荐
- Linux下汇编语言学习笔记36 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- hnuun 11544 小明的烦恼——找字符串(求环形字符串的最小最大字典序)
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11544&courseid=0 最小最大表示法: 求环 ...
- JVM(三):深入分析Java字节码-上
JVM(三):深入分析Java字节码-上 字节码文章分为上下两篇,上篇也就是本文主要讲述class文件存在的意义,以及其带来的益处.并分析其内在构成之一 ---字节码,而下篇则从指令集方面着手,讲解指 ...
- 华为OJ2011-最长公共子串
一.题目描述 描述: 计算两个字符串的最大公共子串(Longest Common Substring)的长度,字符区分大小写. 输入: 输入两个字符串 输出: 输出一个整数 样例输入: asdfas ...
- lua-5.2.3编译问题记录"libreadline.so: undefined reference to `PC'"
作者:zhanhailiang 日期:2014-10-21 [root@~/software]# cd lua-5.2.3 [root@~/software/lua-5.2.3]# make linu ...
- ps图片怎样实现渐变
首先我先将一副图片展示给大家: 我所做的效果是想把图片做成渐变.而不改变中间图片的颜色效果. 接下来我来说明一下我的所做思路. 首先将图片中间部分选择出来.然后复制成一个图层. 详细操作是用魔棒进行反 ...
- IE7下兼容问题总结
1.<LI> border-bottom 不显示 解决办法 加个height:100%; 2.border:none;不好使,要用 border:0;
- UVA-10163 Storage Keepers DP
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- 混合使用Delphi和C ++(附下载)
您想将C ++添加到Delphi应用程序中吗?或者将Delphi代码添加到C ++应用程序中?这是如何做. 您可能不知道的一件事是如何在RAD Studio中集成C ++和Delphi语言.您可以将单 ...
- Struts数据验证
Action类继承了ActionSupport类,而该类实现了Action.Validateable.ValidationAware.TextProvider.LocaleProvider和Seria ...