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?
链接: http://leetcode.com/problems/additive-number/
题解:
求String是否是additive number。这里我们要用到递归。就是先取第一个单词和第二个单词,求出sum,再跟剩下字符串的首部进行比较,然后进行递归。还有很多pruning可以做,比如取num1和num2时只用遍历一半的字符,因为假如这两个的和超过了字符串长的一半,肯定不符合规定。 二刷要注意精炼代码。
Time Complexity - O(2n), Space Complexity - O(2n)
public class Solution {
public boolean isAdditiveNumber(String num) {
if(num == null || num.length() == 0) {
return false;
}
for(int i = 1; i < num.length(); i++) {
for(int j = i + 1; j < num.length(); j++) {
String s1 = num.substring(0, i);
String s2 = num.substring(i, j);
if(isAdditiveNumber(s1, s2, num.substring(j))) {
return true;
}
}
}
return false;
}
private boolean isAdditiveNumber(String s1, String s2, String num) {
if(num.length() == 0) {
return true;
}
if((s1.length() > 1 && s1.charAt(0) == '0') || (s2.length() > 1 && s2.charAt(0) == '0')) {
return false;
}
String sum = getSum(s1, s2);
if(sum.length() > num.length() || !sum.equals(num.substring(0, sum.length()))) {
return false;
} else {
return isAdditiveNumber(s2, sum, num.substring(sum.length()));
}
}
public String getSum(String s1, String s2) {
StringBuilder sb = new StringBuilder();
int index1 = s1.length() - 1, index2 = s2.length() - 1;
int carry = 0, newDigit = 0;
while(index1 >= 0 || index2 >= 0) {
int digitA = index1 >= 0 ? s1.charAt(index1) - '0' : 0;
int digitB = index2 >= 0 ? s2.charAt(index2) - '0' : 0;
newDigit = (digitA + digitB + carry) % 10;
sb.insert(0, newDigit);
carry = (digitA + digitB + carry) >= 10 ? 1 : 0;
index1--;
index2--;
}
if(carry == 1) {
sb.insert(0, '1');
}
return sb.toString();
}
}
题外话:
今天家里修理gas range,修了一天,所以只做了一道题。修理工是一个多米尼加人,最后做的不太好,还要了$700,好气人啊....明天去吃Hakata Tonton,希望能在图书馆多做几道题目。
Reference:
https://leetcode.com/discuss/70124/0ms-concise-solution-perfectly-handles-the-follow-leading
https://leetcode.com/discuss/70102/java-recursive-and-iterative-solutions
https://leetcode.com/discuss/70119/backtracking-with-pruning-java-solution-and-python-solution
https://leetcode.com/discuss/71455/very-straightforward-solution-with-detailed-explanation
https://leetcode.com/discuss/70157/java-easy-understand-dfs
https://leetcode.com/discuss/70089/python-solution
https://leetcode.com/discuss/70796/simple-java-solution
https://leetcode.com/discuss/70123/my-simple-c-non-recursion-solution
https://leetcode.com/discuss/73387/elegant-0ms-backtracking-solution-in-c-with-explanation
306. Additive Number的更多相关文章
- [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 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 s ...
- 306 Additive Number 加法数
Additive number is a string whose digits can form additive sequence.A valid additive sequence should ...
- 【刷题-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 sh ...
- [LeetCode] Additive Number 加法数
Additive number is a positive integer whose digits can form additive sequence. A valid additive sequ ...
- Additive Number
Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...
随机推荐
- 与电子钱包相关的APDU指令
CLS:命令报文的类别字节,class byte(类别字节) of command message(命令报文) UranusPay ED/EP: UranusPay是HB公司开发的COS,而ED是电子 ...
- linux下搭建mysql主从
在master上创建repl账户,用于复制. grant replication slave on *.* to 'repl'@'%' identified by 'P@$$W0rd'; flush ...
- PSP记录表
学生 崔乐乐 日期 2015/3/15 教师 王建民 课程 软件工程 周活动总结表 任务 日期 听课 写程序 ...
- asp.net中父子页面通过gridview中的按钮事件进行回传值的问题
这两天写BS程序,遇到父子页面传值的问题,以前没写过web系统,用了几天时间才将问题解决,总结下记录下来: 问题描述: 父页面A中有一个gridview,每行6个列,有5列中均有一个按钮,单击按钮,会 ...
- python 数据结构-集合set
原文地址:http://docs.pythontab.com/python/python3.4/datastructures.html#tut-tuples 集合是一个无序不重复元素的集. 基本功能包 ...
- IIS和MVC
现象:MVC项目部署到IIS(7.5)后,浏览时只显示文件目录,不是网站 解决办法 1.服务器安装程序对应的Framework版本 2.服务器安装程序对应的MVC版本 3.安装CGI和ISAPI扩展: ...
- 虚拟目录里面的webconfig不继承网站的设置
必須在上一层虚拟目录(如根目录,上级网站)所在的Web.config加上 如:<location path="." allowOverride="false&quo ...
- 利用URLRewriter.dll 实现ASP.NET实现伪静态
大家一定经常在网络上看到很多网站的地址后缀都是用XX.HTML或者XX.ASPX等类似静态文件的标示来操作的吧,那么大家有怀疑过他真的是一个一个的静态生成的文件么,静态文件的生成的优缺有好有坏,对于访 ...
- 从浏览器的console获取angularjs的scope
http://ionicframework.com/blog/angularjs-console/ 1: Access Scopes We can access any scope (even iso ...
- Sqli-labs less 19
Less-19 从源代码中我们可以看到我们获取到的是HTTP_REFERER 那和less18是基本一致的,我们从referer进行修改. 还是像less18一样,我们只给出一个示例 将referer ...