Additive Number

没啥好说的,注意用long(明明题目里说integer,汗)

public class Solution {
    public boolean isAdditiveNumber(String num) {
        int length = num.length();
        int i = 1;
        while (i < length && Long.valueOf(num.substring(0, i)) <= Integer.MAX_VALUE) {
            int j = i + 1;
            while (j <= length && Long.valueOf(num.substring(i, j)) <= Integer.MAX_VALUE) {
                if ((num.substring(0, i).length() > 1 && num.substring(0, i).startsWith("0")) || (num.substring(i, j).length() > 1 && num.substring(i, j).startsWith("0"))) {
                    break;
                }
                long first = Integer.valueOf(num.substring(0, i));
                long second = Integer.valueOf(num.substring(i, j));
                if (num.substring(j).length() == 0) {
                    break;
                }
                if (isvalid(first, second, num.substring(j))) {
                    return true;
                }
                j ++;
            }
            i ++;
        }
        return false;
    }
    public boolean isvalid(long first, long second, String num) {
        if (num.length() == 0) {
            return true;
        }
        long third = first + second;
        if (num.length() >= String.valueOf(third).length() && num.substring(0, String.valueOf(third).length()).equals(String.valueOf(third))) {
            return isvalid(second, third, num.substring(String.valueOf(third).length()));
        }
        return false;
    }
}
时间: 2024-10-27 19:47:51

Additive Number的相关文章

[LeetCode][JavaScript]Additive Number

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 o

306. Additive Number

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. For e

[leetcode] 306. Additive Number 解题报告

题目链接: https://leetcode.com/problems/additive-number/ 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

Leetcode: Additive Number

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. For e

Leetcode 306. Additive Number

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. For e

Additive Number 306

题目链接:https://leetcode.com/problems/additive-number/ 题目描述:一串数字是Additive number,必须存在一个划分将这一串数字分成n个数字,n必须大于3,并且除了前两个数字之外,每个数字都是前两个数字之和,此外,不存在包含前导零的数字. 题目分析:深度搜索 代码实现: #include <map> #include <vector> #include <algorithm> #include <cstrin

306. Additive Number java solutions

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. For e

306 Additive Number 加法数

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.For exa

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.