-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathAdditive Number.js
57 lines (45 loc) · 1.65 KB
/
Additive Number.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
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?
*/
/**
* @param {string} num
* @return {boolean}
*/
var isAdditiveNumber = function(num) {
var len = num.length,
i,
j;
for (i = 1; i <= len / 2; i++) {
for (j = 1; j <= len / 2; j++) {
if (canBeAdded(num.substr(0, i), num.substr(i, j), num.substr(i + j))) {
return true;
}
}
}
return false;
};
function canBeAdded(a, b, c) {
if ((a.length > 1 && a.charAt(0) === '0') || (b.length > 1 && b.charAt(0) === '0') || (c.length >= 1 && c.charAt(0) === '0')) {
return false;
}
var aNum = parseInt(a),
bNum = parseInt(b),
sum = aNum + bNum + '';
if (c === sum) {
return true;
}
if (c.substr(0, sum.length) !== sum) {
return false;
}
return canBeAdded(b, sum, c.substr(sum.length));
}