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;
        }
  • 相关阅读:
    Ubuntu下ATI显卡风扇转速调节脚本
    App_GlobalResources和LocalResources文件夹区别
    ubuntu下添加开机启动项
    UBUNTU安装PHP
    ubuntu apache2配置
    在ubuntu 上安装半透明玻璃窗口
    ASP.NET 页面:在此页面动态调用用户控件(.ASCX)
    Ubuntu 安装 LAMP 主机之后运行出现乱码
    Ubuntu下安装LMAP—菜鸟版
    Ubuntu 10.04风扇声音太大
  • 原文地址:https://www.cnblogs.com/lasclocker/p/4977942.html
Copyright © 2011-2022 走看看