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 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 containing only digits ‘0‘-‘9‘, write a function to determine if it‘s an additive number.

Follow up:
How would you handle overflow for very large input integers?

Credits:
Special thanks to @jeantimex for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

 1 public class Solution {
 2     public boolean isAdditiveNumber(String num) {
 3         int len = num.length();
 4         for(int i = 1; i <= len/2; i++){
 5             for(int j = 1; Math.max(i,j) <= len-i-j;j++){
 6                 if(isValid(i,j,num)) return true;
 7             }
 8         }
 9         return false;
10     }
11
12     public boolean isValid(int i,int j,String num){
13         if(num.charAt(0) == ‘0‘ && i > 1) return false;
14         if(num.charAt(i) == ‘0‘ && j > 1) return false;
15         String sum;
16         Long one = Long.parseLong(num.substring(0,i));
17         Long two = Long.parseLong(num.substring(i,i+j));
18         for(int s = i+j; s < num.length(); s += sum.length()){
19             two = two + one;
20             one = two - one;
21             sum = two.toString();
22             if(!num.startsWith(sum,s)) return false;
23         }
24         return true;
25     }
26 }

递归解法:

https://discuss.leetcode.com/topic/50580/java-recursive-simple-dfs-solution

https://discuss.leetcode.com/topic/29856/java-recursive-and-iterative-solutions

时间: 2024-10-09 08:44:26

306. Additive Number java solutions的相关文章

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 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

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

9. Palindrome Number Java Solutions

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

Additive Number 306

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

63. Unique Paths II java solutions

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

[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

【LeetCode】Single Number (2 solutions)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl