zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    bash 设置
    fabric-ca-client
    fabric工具
    nodejs
    Hyperledger 项目
    JAVA集合小结
    java内部类的作用分析
    如何使用javadoc
    30天轻松学习javaweb_Eclipse在修改了web.xml后将自动更新到tomcat服务器中
    30天轻松学习javaweb_修改tomcat的servlet模板
  • 原文地址:https://www.cnblogs.com/lishiblog/p/5792622.html
Copyright © 2011-2022 走看看