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.
分析:
递归。先确定前两个数,第三个数的位置由前两个数决定。
public class Solution {
public boolean isAdditiveNumber(String num) {
if (num.length() < ) return false;
for (int i = ; i <= num.length() - ; i++) {
for (int j = i + ; j <= num.length() - ; j++) {
if (helper(num, i, j - i)) {
return true;
}
}
}
return false;
}
private boolean helper(String num, int num1Length, int num2Length) {
if (num.length() <= num1Length + num2Length) return false;
String number1 = num.substring(, num1Length);
String number2 = num.substring(num1Length, num1Length + num2Length);
if (number1.length() > && number1.charAt() == '') return false;
if (number2.length() > && number2.charAt() == '') return false;
for (int i = num1Length + num2Length + ; i <= num.length(); i++) {
String number3 = num.substring(num1Length + num2Length, i);
if (number3.length() > && number3.charAt() == '') return false;
if (Long.parseLong(number1) + Long.parseLong(number2) == Long.parseLong(number3)) {
if (i == num.length()) {
return true;
} else if (helper(num.substring(num1Length), num2Length, i - num1Length - num2Length)) {
return true;
}
} else if (Long.parseLong(number1) + Long.parseLong(number2) < Long.parseLong(number3)) {
break;
}
}
return false;
}
}
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
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 [Medium]
306. Additive Number class Solution { private: string stringAddition(string &a, string &b) { ...
- 【LeetCode】306. Additive Number
题目: Additive number is a string whose digits can form additive sequence. A valid additive sequence s ...
- [Swift]LeetCode306. 累加数 | Additive Number
Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...
- LeetCode(306) Additive Number
题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...
- 306 Additive Number 加法数
Additive number is a string whose digits can form additive sequence.A valid additive sequence should ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
随机推荐
- AspnetIdentitySample
https://github.com/rustd/AspnetIdentitySample http://www.asp.net/web-forms/overview/getting-started/ ...
- [Html5]sessionStorage和localStorage常见操作
摘要 [Html5]sessionStorage和localStorage的区别 索引 上篇文章简单介绍了它们的区别,已经常见的用法.那我们能通过. 或者类似dic[key]的方式访问吗?答案是当然可 ...
- PPTP(Point to Point Tunneling Protocol),即点对点隧道协议。
PPTP PPTP(Point to Point Tunneling Protocol),即点对点隧道协议.该协议是在PPP协议的基础上开发的一种新的增强型安全协议,支持多协议虚拟专用网(VPN),可 ...
- Win8/Win10无法打开这个应用 内置管理员账户
现在装win10系统的同伴越来越多了,相比于win7,win10在某些设置方面也有些变化,比如我们在使用win8或者win10时,会碰到如图所示的对话框: Windows10/Windows8无法使用 ...
- js中的换算小技巧
之前自己一直使用~~运算符来把‘112222’字符型的数值换算成整型的数值 但今天调试程序发现了一些问题 ~~'999'=>999 ~~'111111999'=>111111999 这些都 ...
- [译]Mongoose指南 - 查询
查询有带callback和不带callback两种方式 所有mongoose的callback都是这种格式: callback(err, result) var Person = mongoose.m ...
- 电脑开机黑屏,显示Reboot and Select proper boot device!
“reboot and select proper boot device or insert boot media in selected boot device and press a key” ...
- C#深入浅出 C#语法中的重中之重——委托(四)
入行半年多了,委托干什么用的还不知道,真心说不过去了,关键对这东西有点恐惧,主要是被老师吓的,记得我C#专业课老师在讲到委托时,原话是这样的,同学们,委托这个地方是难点,暂时不讲,讲了你也不懂,等你有 ...
- Unix时间戳转换怎样在Excel批量修改?
最近在操作项目的时候碰到一个Unix时间戳转换的问题."date_time":1393031347这个是什么,你知道吗?如果你对Unix时间戳了解的话一眼就看出来.但我们本着科普的 ...
- Java中将unix时间戳转化为正常显示时间
在unix中时间戳是一串数字表示的,使用起来非常不方便,转化方式如下: //Convert Unix timestamp to normal date style public String Time ...