题目:

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的更多相关文章

  1. [LeetCode] 306. Additive Number [Medium]

    306. Additive Number class Solution { private: string stringAddition(string &a, string &b) { ...

  2. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  3. Leetcode 306. Additive Number

    Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...

  4. 【LeetCode】306. Additive Number

    题目: Additive number is a string whose digits can form additive sequence. A valid additive sequence s ...

  5. 306 Additive Number 加法数

    Additive number is a string whose digits can form additive sequence.A valid additive sequence should ...

  6. 【刷题-LeetCode】306. Additive Number

    Additive Number Additive number is a string whose digits can form additive sequence. A valid additiv ...

  7. LeetCode(306) Additive Number

    题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...

  8. [LeetCode] Additive Number 加法数

    Additive number is a positive integer whose digits can form additive sequence. A valid additive sequ ...

  9. Additive Number

    Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...

随机推荐

  1. 针对《来用》的NABC分析

    项目名:<来用> 特点:拥有以往win7在内的众多小游戏 NABC分析 N(need需求): 之所以有这个想法是因为,在WIN7,XP系统中往往有很多众所周知的小游戏(比如扫雷),但是在w ...

  2. (转)c语言学习volatile

    原文网址:http://www.cnblogs.com/chio/archive/2007/11/24/970632.html 参考网址:http://www.embedu.org/Column/Co ...

  3. Xamarin.Android之转换,呼叫,查看历史纪录

    Xamarin.Android之转换,呼叫,查看历史纪录 E文文章. 功能:能将输入的字母转换成相应的数字.并且能呼叫出去.能查看呼叫的历史纪录. 界面代码如下: <?xml version=& ...

  4. android 图片缩放抗锯齿

    之前用的时候只设置了antialias属性,其实要设置两个flag才行 paint.setFlags(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG); ...

  5. LineNumberReader类

    开发人员常常会随手写一些类来读入一个简单的配置文件,或者用BufferedReader一行一行地读入特定格式的数据.这些随手写成的解析器往 往会提供基本的出错报告,但有时候它们会很难记录下出错的行号. ...

  6. hibernate--OneToOne

    一对一(one to one) 单向关联映射 两个对象是一对一的的关系. 有两种策略可以实现一对一的关联映射 l  主键关联:即让两个对象具有相同的主键值,以表明他们之间的一对一的对应关系;数据库表不 ...

  7. poj 1113 Wall

    题目链接:http://poj.org/problem?id=1113 题目大意:给出点集和一个长度L,要求用最短长度的围墙把所有点集围住,并且围墙每一处距离所有点的距离最少为L,求围墙的长度. 解法 ...

  8. 【BZOJ】【3156】防御准备

    DP/斜率优化 斜率优化的裸题…… sigh……又把$10^6$当成10W了……RE了N发 这题还是很水的 当然逆序也能做……不过还是整个反过来比较顺手 反转后的a[0]=反转前的a[n],以此类推直 ...

  9. 【BZOJ】【1001】 【BJOI2006】狼抓兔子

    平面图最小割->对偶图最短路 平面图最小割转对偶图最短路= = 想到了就比较好写了…… 可能是我对区域的标号方式比较奇特?反正我没有特判n==1||m==1也能过2333(机智吧-(滚开啦你个自 ...

  10. vector内存分配

    vector,map 这些容器还是在堆上分配的内存,在析构时是释放空间 vector在提高性能可以先reserve在push_back() reserve:决定capacity,但没有真正的分配内存, ...