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: ...
随机推荐
- yii2嵌入微信公众号支付
原文地址:https://segmentfault.com/a/1190000005114556
- 常用的MIME类型
.doc application/msword .docx application/vnd.openxmlformats-officedocument.wordprocessingml.d ...
- Linux服务器管理: 系统的进程管理top命令
查看系统运行状态的命令top [root@localhost~]#top [选项] 选项: -d 秒数 指定top命令每个几秒更新.默认为3秒 在top命令的交互模式当中可以执行的命令 ?或h 查看帮 ...
- UTF-8-BOM
https://www.zhihu.com/question/20167122 知乎 「带 BOM 的 UTF-8」和「无 BOM 的 UTF-8」有什么区别?网页代码一般使用哪个? http://b ...
- flask 知识点总结
============================request对象的常用属性============================具体使用方法如下:request.headers, requ ...
- sql server cpu占用过高优化
SQLSERVER排查CPU占用高的情况 今天中午,有朋友叫我帮他看一下数据库,操作系统是Windows2008R2 ,数据库是SQL2008R2 64位 64G内存,16核CPU 硬件配置还是比较高 ...
- 由json字符串生成C#实体类的工具
json作为互联网上轻量便捷的数据传输格式,越来越受到重视.但在服务器端编程过程中,我们常常希望能通过智能提示来提高编码效率.JSON C# Class Generator 能将json格式所表示的J ...
- Linq→join中指定多个条件
还是习惯先撸一段SQL * FROM User_Pic P AND P.Guid = R.UserPicGuid ORDER BY PicSize DESC 然后发现Linq中的join不能多条件.. ...
- oracle 使用ID关键字作列名导致索引失效
oracle表空间变更导致主键索引失效,重建索引即可
- ZipArchive 打包下载压缩包
用php的header()方式下载压缩包. 要点:1.不能在header导出压缩包前向浏览器输出内容,否则文件下载压缩包成功,打开的压缩包也会显示被破坏. 2.在压缩文件包的php代码前不可以有js脚 ...