LeetCode 306. Additive Number(搜索)

306. Additive Number

Medium

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

题意

给定一个数字组成的字符串,判断是否可以分割成数字序列,使得序列中每个数字都是由前两个数字相加而成(第一个和第二个数字除外)。含有先导零的数字不合法(例如03是非法数字)。

思路

搜索 + 简单的剪枝(第一个数字和第二个数字的长度不可能超过字符串剩余长度的一半)。

代码

class Solution {
    /**
    @param str Input string
    @param begin Begin index of this recursion
    @param expected Expected value of this recursion
    @param prev Previous value
    @return If is additive number
    */
    private boolean recursive(String str, int begin, long expected, long prev) {
        if (begin == str.length()) {
            return true;
        }
        int n = str.length() - begin, i = begin, bound = n;
        if (prev == -1 && expected == -1) {  // first recusion
            bound = n/2;
            for (i=begin+1; i<=begin+bound; ++i) {
                if (i > begin+1 && str.charAt(begin) == '0') {
                    continue;
                }
                if (recursive(str, i, -1, Long.parseLong(str.substring(begin, i)))) {
                    return true;
                }
            }
        } else if (expected == -1) {    // second recusion
            bound = n/2;
            for (i=begin+1; i<=begin+bound; ++i) {
                if (i > begin+1 && str.charAt(begin) == '0') {
                    continue;
                }
                long tmp = Long.parseLong(str.substring(begin, i));
                if (recursive(str, i, tmp+prev, tmp)) {
                    return true;
                }
            }
        } else {
            bound = n;
            for (i=begin+1; i<=begin+bound; ++i) {
                if (i > begin+1 && str.charAt(begin) == '0') {
                    continue;
                }
                long tmp = Long.parseLong(str.substring(begin, i));
                if (tmp == expected && recursive(str, i, tmp+prev, tmp)) {
                    return true;
                }
            }
        }
        return false;
    }
    
    public boolean isAdditiveNumber(String num) {
        return recursive(num, 0, -1, -1);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值