leetcode Additive Number

原创 2016年08月30日 19:24:29

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?

Credits:

Special thanks to @jeantimex for adding this problem and creating all test cases.

Brute Force,要注意的是题目中的提示,以0开头的两位数或多位数是不合法的,还有就是循环的起止条件,两数相加等于第三数,第三个数的长度一定是大于等于前两个数之中长度最长的数的,代码:

public boolean isAdditiveNumber(String num) {
      int len=num.length();
    for(int i=1;i<=len/2;i++){
        for(int j=1;Math.max(i,j)<=len-i-j;j++){
            if(isAddi(num,i,j)) return true;
        }
    }
    return false;
}
public boolean isAddi(String num,int i,int j){
    if(num.charAt(0)=='0'&&i>1) return false;
    if(num.charAt(i)=='0'&&j>1) return false;
    Long n1=Long.parseLong(num.substring(0,i));
    Long n2=Long.parseLong(num.substring(i,i+j));
    String sum="";
    for(int k=i+j;k!=num.length();k+=sum.length()){
        n2=n1+n2;
        n1=n2-n1;
        sum=n2.toString();
        if(!num.startsWith(sum,k)) return false;
    }
    return true;
}

[LeetCode306] Additive Number

Additive number is a string whose digits can form additive sequence. A valid additive sequence sh...
  • sbitswc
  • sbitswc
  • 2015年12月06日 03:56
  • 2840

LeetCode306——Additive Number且看我是如何解决的

Additive number is a string whose digits can form additive sequence. A valid additive sequence sh...
  • booirror
  • booirror
  • 2015年12月05日 22:23
  • 1637

[leetcode] 306. Additive Number

题目: Additive number is a string whose digits can form additive sequence.A valid additive sequence s...
  • u014673347
  • u014673347
  • 2016年02月21日 11:29
  • 682

LeetCode(306) Additive Number

题目Additive number is a string whose digits can form additive sequence.A valid additive sequence shou...
  • fly_yr
  • fly_yr
  • 2015年12月12日 16:56
  • 1972

[LeetCode]Additive Number

题目链接:Additive Number题目内容:Additive number is a positive integer whose digits can form additive sequen...
  • xyt8023y
  • xyt8023y
  • 2015年11月23日 14:34
  • 1498

LeetCode:306. Additive Number

原题: Additive number is a string whose digits can form additive sequence. A valid additive sequence...
  • x_i_y_u_e
  • x_i_y_u_e
  • 2016年02月23日 16:53
  • 1196

LeetCode 306 Additive Number

Additive number is a string whose digits can form additive sequence. A valid additive sequence shou...
  • niuooniuoo
  • niuooniuoo
  • 2016年05月18日 11:03
  • 390

306. Additive Number LeetCode

题意:给出一个只包含数字的字符串,问是否可以分成至少3个数,前两个数相加等于第三个数。 题解:枚举前两项,有了前两项就可以递推出后面的值,然后递推到跟原字符串长度相等的时候停止,然后比较是否相等,相...
  • youthinkwu
  • youthinkwu
  • 2016年03月16日 17:56
  • 220

【LeetCode】306. Additive Number

题目描述: Additive number is a string whose digits can form additive sequence. A valid additive sequenc...
  • MapleJam
  • MapleJam
  • 2016年10月18日 08:42
  • 120

Additive Number | Leetcode

Additive number is a positive integer whose digits can form additive sequence. A valid additive seq...
  • jiangbo1017
  • jiangbo1017
  • 2015年11月18日 19:43
  • 554
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Additive Number
举报原因:
原因补充:

(最多只允许输入30个字)