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

https://leetcode.com/problems/additive-number/



明天写思路。

 1 /**
 2  * @param {string} num
 3  * @return {boolean}
 4  */
 5 var isAdditiveNumber = function(num) {
 6     var len = num.length, tail;
 7     for(var i = 1; i <= parseInt(num.length / 2); i++){
 8         tail = num.substring(len - i, len);
 9         if(!isValidNum(tail)) continue;
10         if(checkTailNum(num.substring(0, len - i), tail)) return true;
11     }
12     return false;
13
14     function isValidNum(num){
15         if(num.length >= 2 && tail[0] === ‘0‘) return false;
16         return true;
17     }
18     function checkTailNum(str, number){
19         var len = str.length, numA, numB, i, j;
20         for(i = 1; i <= number.length; i++){
21             numA = str.substring(len - i, len);
22             if(!isValidNum(numA)) continue;
23             for(j = 1; j <= number.length; j++){
24                 numB = str.substring(len - i - j, len - i);
25                 if(!isValidNum(numB)) continue;
26                 if(parseInt(numA) + parseInt(numB) === parseInt(number)){
27                     if(str.substring(0, len - i - j) === "") return true;
28                     return checkTailNum(str.substring(0, len - i), str.substring(len - i, len));
29                 }
30             }
31         }
32         return false;
33     }
34 };
时间: 2024-10-18 13:30:28

[LeetCode][JavaScript]Additive Number的相关文章

[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

[LeetCode][JavaScript]Happy Number

Happy Number Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the proc

[LeetCode][JavaScript]Single Number II

Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? https://leetcode.com/

[LeetCode][JavaScript]Single Number

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? https://leetcode.com/problems/

[LeetCode][JavaScript]Single Number III

Single Number III Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]

[LeetCode][JavaScript]Valid Number

https://leetcode.com/problems/valid-number/ Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true Note: It is

[LeetCode][JavaScript]Largest Number

Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string

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