[Leetcode学习-java]Additive Number

问题:

难度:medium

说明:

给出一个字符串,"112358" 这样的数字字符串,其中存在一个规律就是 前两个 数字 相加 等于后一个数字,就是说 1 + 1 = 2, 所以排列 112 ,然后 1 + 2 = 3 所以 1123,然后 2 + 3 = 5,所以排列 11235,最后 3 + 5 = 8 ,所以排列 112358,那么现在要求输入一个字符串,判断是否是这样子 前两个相加 等于 后一个 的规律,其中不允许有非法 0,比如说 00 01 02 这种都是不行的。

题目连接:https://leetcode.com/problems/additive-number/

输入范围:

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

输入案例:

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

我的代码:

其实可以认为是开头决定了结果,那么获取所有可能的开头进行遍历求解,就能判断是否是符合规律的字符。

首先因为 35 长度,所以不必怕暴力超时,此外要注意 Long 最大值也就 19 字符长度,35 还是会超,幸好 18 + 18 = 36 也超长了,这就意味着只开头的字符截取,需要长度在 35 / 3 = 11 这个范围内就行。

如果说继续超 35 那么就可能得要大数运算,并且便利情况还可能超时。

class Solution {
        private int index;
        public boolean isAdditiveNumber(String num) {
            if(num.length() < 3) return false;
            int maxLen = num.length() > 13 ? 13 : num.length();
            long[][] beginer = new long[(maxLen - 1) * (maxLen - 2) / 2][2];
            String[][] beginerStr = new String[(maxLen - 1) * (maxLen - 2) / 2][2];
            for(int i = 1, len1 = maxLen - 1; i < len1; i ++)
                for(int j = 1, len2 = maxLen - i; j < len2; j ++)
                    buildBegin(beginer, beginerStr, num, i, j);

            boolean success = false;
            long[] cache = new long[2];
            String[] cacheStr = new String[2];
            A:for(int i = 0, begin = 0, end = 0, len = num.length(); i < index; i ++, begin = 0, end = 0) {
                cache[0] = beginer[i][0];
                cache[1] = beginer[i][1];
                cacheStr[0] = beginerStr[i][0];
                cacheStr[1] = beginerStr[i][1];

                while(end < len) {
                    long next = cache[0] + cache[1];
                    String nextStr = String.valueOf(next);
                    int nextb = cacheStr[0].length() + cacheStr[1].length();
                    if(begin + nextb + nextStr.length() <= len &&
                       num.substring(begin + nextb, begin + nextb + nextStr.length()).equals(nextStr)) {
                        end = begin + nextb + nextStr.length();
                        begin += cacheStr[0].length();
                        cache[0] = cache[1];
                        cache[1] = next;
                        cacheStr[0] = cacheStr[1];
                        cacheStr[1] = nextStr;
                    } else continue A;
                }
                success = true;
                break;
            }
            return success;
        }

        private void buildBegin(long[][] beginer, String[][] beginerStr, String num, int first, int second) {
            String firstStr = num.substring(0, first);
            String secondStr = num.substring(first, first + second);
            if(first > 1 && firstStr.charAt(0) == '0') return;
            if(second > 1 && secondStr.charAt(0) == '0') return;
            beginer[index][0] = Long.valueOf(firstStr);
            beginer[index][1] = Long.valueOf(secondStr);
            beginerStr[index][0] = firstStr;
            beginerStr[index ++][1] = secondStr;
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值