-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path306.累加数.js
53 lines (45 loc) · 1.11 KB
/
306.累加数.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
/*
* @lc app=leetcode.cn id=306 lang=javascript
*
* [306] 累加数
*/
// @lc code=start
/**
* @param {string} num
* @return {boolean}
*/
var isAdditiveNumber = function(num) {
function dfs(index, nextIndex) {
if (nextIndex === num.length) {
return true
}
let result = false
for(let i = index + 1; i < num.length; i++) {
let item1 = num.slice(index, i);
if (item1.length > 1 && item1[0] == '0') {
continue
}
for (let j = i +1; j < num.length; j++) {
let item2 = num.slice(i, j);
if (item2.length > 1 && item2[0] == '0') {
continue
}
for (let k = j + 1; k <= num.length; k++) {
let item3 = num.slice(j, k);
if (item3.length > 1 && item3[0] == '0') {
continue
}
console.log(item1, item2, item3)
if (parseInt(item1) + parseInt(item2) === parseInt(item3)) {
result = result || dfs(i, k)
}
}
}
}
console.log(index, result)
return result
}
return dfs(0, 0);
};
// @lc code=end
console.log(isAdditiveNumber('199100199'))