-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathAdditive Number.java
35 lines (30 loc) · 1.09 KB
/
Additive Number.java
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
class Solution {
public boolean isAdditiveNumber(String num) {
return backtrack(num, 0, 0, 0, 0);
}
public boolean backtrack(String num, int idx, long sum, long prev, int length){
if(idx == num.length()){
return length >= 3;
}
long currLong = 0;
for(int i = idx; i < num.length(); i++){
//make sure it won't start with 0
if(i > idx && num.charAt(idx) == '0') break;
currLong = currLong * 10 + num.charAt(i) - '0';
if(length >= 2){
if(sum < currLong){
//currLong is greater than sum of previous 2 numbers
break;
}else if(sum > currLong){
//currLong is smaller than sum of previous 2 numbers
continue;
}
}
//currLong == sum of previous 2 numbers
if(backtrack(num, i + 1, currLong + prev, currLong, length + 1) == true){
return true;
}
}
return false;
}
}