zoukankan      html  css  js  c++  java
  • [leetcode] Additive Number

    题目:

    Additive number is a positive integer 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 represents an integer, write a function to determine if it's an additive number.
    
    Follow up:
    How would you handle overflow for very large input integers?

    思路:利用DSP的思想,首先利用两层循环,固定第一个数first和第二个数second,之后遍历剩余的字符串,看是否能找到第三个数third,使first+second=third,

    如果找到满足条件的third,则递归判断余下的字符串是否满足second+third=fourth,一直递归,若到达字符串的末尾,则返回true,否则返回false。

    比如:对"199100199",可以细分为下面的过程:

    1, 9, 9, 91, 910, ......

    1, 99, 1, 10, 100, ......

    1, 991, 0, 00, 001, ......

    .......

    19, 9, 1, 100, 1001, .......

    Java代码:

        public boolean isAdditiveNumber(String num) {
            if (num.length() < 3) {
                return false;
            }
            for (int i = 0; i < num.length()-2; i++) {
                String first = num.substring(0, i+1);
                if (! isLong(first)) break;
                for (int j = i+1; j < num.length()-1; j++) {
                    String second = num.substring(i+1, j+1);
                    if (! isLong(second)) break;
                    if(operate(num, j+1, Long.parseLong(first), Long.parseLong(second))) {
                        return true;
                    }
                }
            }
            return false;
        }
    
        private boolean operate(String num, int cur, long first, long second) {
            if (cur == num.length()) {
                return true;
            }
            for (int i = cur; i < num.length(); i++) {
                String third = num.substring(cur, i+1);
                if (! isLong(third)) break;
                if (first + second < Long.parseLong(third)) {
                    break;
                }
                if (first + second == Long.parseLong(third)) {
                    return operate(num, i+1, second, Long.parseLong(third));
                }
            }
            return false;
        }
        
        private boolean isLong(String num) { //此函数用于解决Follow up中的问题
            if (num.charAt(0) == '0' && num.length() > 1) {
                return false;
            }
            if (num.length() > 19) {
                return false;
            }
            if (num.length() == 19 && num.compareTo(Long.toString(Long.MAX_VALUE)) > 0) {
                return false;
            }
            return true;
        }
  • 相关阅读:
    登录Mysql看不到Mysql库
    七牛云使用记录
    FFmpeg工具
    解决VMware下CentOS连不上网络问题
    14.中介者模式
    二十三种设计模式(三)
    23种设计模式(二)
    搭建ssm环境
    文件的字符流与字节流读写
    设计模式之用工厂模式实现计算器
  • 原文地址:https://www.cnblogs.com/lasclocker/p/4977942.html
Copyright © 2011-2022 走看看