C#版 - Leetcode 306. 累加数 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址
http://blog.csdn.net/lzuacm。
C#版 - Leetcode 306. 累加数 - 题解
306.Additive Number
在线提交:
https://leetcode-cn.com/problems/additive-number/
累加数是一个字符串,组成它的数字可以形成累加序列。
一个有效的累加序列必须至少包含 3 个数。除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相加的和。
给定一个只包含数字 '0'-'9' 的字符串,编写一个算法来判断给定输入是否是累加数。
说明: 累加序列里的数不会以 0 开头,所以不会出现 1, 2, 03 或者 1, 02, 3 的情况。
示例 1:
输入: "112358"
输出: true
解释: 累加序列为: 1, 1, 2, 3, 5, 8 。1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
示例 2:
输入: "199100199"
输出: true
解释:
累加序列为: 1, 99, 100, 199。1 + 99 = 100, 99 + 100 = 199
进阶:
你如何处理一个溢出的过大的整数输入?
题目难度:
Medium通过次数:68
提交次数:296
相关话题 回溯算法
相似题目 将数组拆分成斐波那契序列
思路:
Additive Number(累计数)的定义类似于Fibonacci数列,先将其划分为字串,用数学公式可将其满足的关系表示为: substrn=substrn−2" role="presentation">substrn=substrn−2substrn=substrn−2 +(concat) substrn−1" role="presentation">substrn−1substrn−1.
可用暴力搜索(Brute Force search)的方法来求解,让第一个数字先从第1位开始,第2个数字从第1位,第2位,往高位开始搜索,前两个数字确定了,相加得到第3位数字,3个数拼接起来形成一个字符串,和原字符串长度相比,如果小于原长度,则取出上一次计算的第2个和第3个数当做新一次计算的前两个数做同样的操作得到新字符串,再和原字符串长度相比…依此类推,直到当前字符串长度不小于原字符串长度,比较两者是否相同,相同返回true,不同则继续循环。如果遍历完了还是没有返回true,则返回false。
已AC代码(使用迭代,这里而不是递归):
public class Solution
{
public bool IsAdditiveNumber(string num)
{
if (String.IsNullOrEmpty(num))
return false;
int len = num.Length;
for (int i = 1; i < len; i++) // 使用两个游标i, j分别作为substr{n-2}和substr{n-1}的开始位置
{
for (int j = i + 1; j < len; j++)
{
string s1 = num.Substring(0, i); // Substring(startIndex, length)
string s2 = num.Substring(i, j - i);
var d1 = long.Parse(s1);
var d2 = long.Parse(s2);
var next = d1 + d2;
if ((s1.Length > 1 && s1[0] == '0') || (s2.Length > 1 && s2[0] == '0'))
continue;
var now = s1 + s2 + next.ToString();
//if (num.IndexOf(now) >= 0)
//{
while (now.Length < num.Length) // Move forward
{
d1 = d2;
d2 = next;
next = d1 + d2;
now += next.ToString();
}
//}
if (now == num)
return true;
}
}
return false;
}
}
Rank:
You are here! Your runtime beats 77.78% of csharp submissions.
有点疑惑的是为何加不加原字符串含是否有新串的判断,对结果没影响呢?欢迎留言交流~
C#版 - Leetcode 306. 累加数 - 题解的更多相关文章
- C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- C#版 - Leetcode 633. 平方数之和 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- Leetcode 10. 正则表达式匹配 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- Java实现 LeetCode 306 累加数
306. 累加数 累加数是一个字符串,组成它的数字可以形成累加序列. 一个有效的累加序列必须至少包含 3 个数.除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相加的和. 给定一个只包含数字 ...
- C#版 - Leetcode 504. 七进制数 - 题解
C#版 - Leetcode 504. 七进制数 - 题解 Leetcode 504. Base 7 在线提交: https://leetcode.com/problems/base-7/ 题目描述 ...
- C#版 - Leetcode 13. 罗马数字转整数 - 题解
C#版 - Leetcode 13. 罗马数字转整数 - 题解 Leetcode 13. Roman to Integer 在线提交: https://leetcode.com/problems/ro ...
- C#版 - Leetcode 201. 数字范围按位与(bitwise AND) - 题解
C#版 - Leetcode 201. 数字范围按位与(bitwise AND) - 题解 在线提交: https://leetcode.com/problems/bitwise-and-of-num ...
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- C#版 - Leetcode 593. 有效的正方形 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
随机推荐
- Invitation Cards POJ - 1511 (双向单源最短路)
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- 怎样在ASP.NET(C#) 使用Json序列化反序列化问题?
using System; using System.Collections.Generic; using System.Web; using System.Web.Script.Serializat ...
- go片段代码
关于枚举类型
- LOJ.6435.[PKUSC2018]星际穿越(倍增)
LOJ BZOJ 参考这儿qwq. 首先询问都是求,向左走的最短路. \(f[i][j]\)表示从\(i\)走到\(j\)最少需要多少步.表示这样只会\(O(n^2\log n)\)的= =但是感觉能 ...
- arp断网攻击
arp断网攻击可以是同局域网内主机无法上网!(可恶搞室友哦,嘻嘻!) 实现原理 arp的中文释义是地址解析协议,全英文 address resolution protocol,是一个将局域网IP地址映 ...
- django默认模板引擎和jinja2模板引擎
在使用中,大家会发现django默认模板引擎有很多局限性,最明显的就是四则运算.就只能加减,乘除都不支持.另外还有判断相等,不能直接if,要用ifequal.确实不太方便.还有一点,django默认模 ...
- PeopleSoft 后台更新密码
一.SQL脚本 where t.oprid='&opridName' ; 二.Data Mover 1.指定用户加密ENCRYPT_PASSWORD 用户名;2.所有用户ENCRYPT_PAS ...
- python学习:购物车程序
购物车程序 product_list = [ ('mac',9000), ('kindle',800), ('tesla',900000), ('python book',105), ('bike', ...
- IE控件cab包手动安装
一.XP系统 第1步:先解压cab包,在解压的文件中找到*.inf文件,然后右击,选择安装,此时会把解压文件拷到C:Windows\System32文件夹下.第2步:注册拷到上述文件夹下的ocx文件. ...
- centos7 ping不通 name or service not known
最近打算为centos安装一个界面时,发现不能上网.ping www.baidu.com 报name or service not known. 原来网络配置没设好. 一.选择VMWare的NAT模式 ...