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;
        }
  • 相关阅读:
    Solr6.6环境安装及core的创建(win7环境)
    使用Druid作为数据源
    Windows远程数据同步工具cwRsync
    解读zookeeper的配置项
    堵塞与非堵塞原理
    Apache Hadoop2.0之HDFS均衡操作分析
    转到简书去了
    淘宝技术这十年概要
    Facebook广告API系列 Business Manager
    Facebook广告API系列 3 Ads Management
  • 原文地址:https://www.cnblogs.com/lasclocker/p/4977942.html
Copyright © 2011-2022 走看看