LeetCode-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?

Analysis:

All varieties come from the first two numbers of the input string. If that is determined, all the followings are just confirming the addition sequence. 

Solution:

 1 public class Solution {
 2    public boolean isAdditiveNumber(String num) {
 3         if (num.length() < 3)
 4             return false;
 5 
 6         for (int len1 = 1; len1 <= num.length() / 2; len1++) {
 7             int maxLen2 = Math.min(num.length() - 2 * len1, (num.length() - len1) / 2);
 8             for (int len2 = 1; len2 <= maxLen2; len2++)
 9                 if (isAddNumRecur(num, 0, len1, len1 + len2)) {
10                     return true;
11                 }
12         }
13         return false;
14     }
15 
16     // Is num an additive number, if we set the first and second number as
17     // num(p1,p2-1) and num(p2,p3-1).
18     public boolean isAddNumRecur(String num, int p1, int p2, int p3) {
19         if (p3 == num.length())
20             return true;
21 
22         
23         // Get the two numbers to start with and get their sum.
24         long num1 = Long.parseLong(num.substring(p1, p2));
25         long num2 = Long.parseLong(num.substring(p2, p3));
26         // the two numbers cannot start with '0' if they are not 0
27         if ((num1!=0 && num.charAt(p1) == '0') || (num2!=0 && num.charAt(p2) == '0'))
28             return false;
29         
30         long num3 = num1 + num2;
31         String num3Str = Long.toString(num3);
32         if (num3Str.length() > num.length() - p3)
33             return false;
34 
35         // If starting from p3, we can find a number that equals to num(p1,p2-1)
36         // + num(p2,p3-1)
37         for (int i = 0; i < num3Str.length(); i++)
38             if (num.charAt(p3 + i) != num3Str.charAt(i)) {
39                 return false;
40             }
41 
42         return isAddNumRecur(num, p2, p3, p3 + num3Str.length());
43     }
44 }

 

转载于:https://www.cnblogs.com/lishiblog/p/5792622.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值