zoukankan      html  css  js  c++  java
  • LeetCode 306. Additive Number

    原题链接在这里:https://leetcode.com/problems/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.

    Given a string containing only digits '0'-'9', write a function to determine if it's an additive number.

    Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.

    Example 1:

    Input: "112358"
    Output: true
    Explanation: 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
    

    Example 2:

    Input: "199100199"
    Output: true
    Explanation: The additive sequence is: 1, 99, 100, 199. 
                 1 + 99 = 100, 99 + 100 = 199

    Constraints:

    • num consists only of digits '0'-'9'.
    • 1 <= num.length <= 35

    题解:

    Get the first two numbers. And check if their sum could be got from the rest.

    DFS state needs first number, second number and rest string.

    If first 2 numbers are legal, calculate the sum.

    If sum is equal to rest, return true.

    Otherwise, if sum could be get from rest string. Continue DFS with 2nd, sum and new rest.

    When getting the first 2 numbers, neither of them length could be larger than half of num length.

    Time Complexity: exponential.

    Space: O(n). n =sum.length(). stack space.

    AC Java:

     1 class Solution {
     2     public boolean isAdditiveNumber(String num) {
     3         if(num == null || num.length() < 3){
     4             return false;
     5         }
     6         
     7         for(int i = 1; i<=num.length()/2; i++){
     8             for(int j = 1; j<=num.length()/2; j++){
     9                 if(dfs(num.substring(0, i), num.substring(i, i+j), num.substring(i+j))){
    10                     return true;
    11                 }
    12             }
    13         }
    14         
    15         return false;
    16     }
    17     
    18     private boolean dfs(String a, String b, String c){
    19         if((a.length() > 1 && a.charAt(0) == '0') || (b.length() > 1 && b.charAt(0) == '0')){
    20             return false;
    21         }
    22         
    23         String sum = getSum(a, b);
    24         if(sum.equals(c)){
    25             return true;
    26         }
    27         
    28         if(sum.length() >= c.length() || !c.substring(0, sum.length()).equals(sum)){
    29             return false;
    30         }
    31         
    32         return dfs(b, c.substring(0, sum.length()), c.substring(sum.length()));
    33     }
    34     
    35     private String getSum(String a, String b){
    36         StringBuilder sb = new StringBuilder();
    37         int i = a.length()-1;
    38         int j = b.length()-1;
    39         int carry = 0;
    40         while(i>=0 || j>=0 || carry>0){
    41             int temp = (i>=0 ? a.charAt(i)-'0' : 0) + (j>=0 ? b.charAt(j)-'0' : 0) + carry;
    42             sb.insert(0, temp%10);
    43             carry = temp/10;
    44             i--;
    45             j--;
    46         }
    47         
    48         return sb.toString();
    49     }
    50 }

    类似Split Array into Fibonacci Sequence.

  • 相关阅读:
    Lombok 安装、入门
    Centos 6.5中使用yum安装jdk
    Mysql规范和使用注意点(转)
    Java编码规范
    windows2016重新配置sid
    Postman POST接口请求无法获取数据
    微信修改密码
    SQL事务
    .net时间格式化
    SQL查询列是否有重复值
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11841130.html
Copyright © 2011-2022 走看看