3
public boolean isAdditiveNumber(String num) {4
return backtrack(num, 0, 0, 0, 0);7
public boolean backtrack(String num, int idx, long sum, long prev, int length) {8
if (idx == num.length()) {14
for (int i = idx; i < num.length(); i++) {15
// make sure it won't start with 016
if (i > idx && num.charAt(idx) == '0') break;17
currLong = currLong * 10 + num.charAt(i) - '0';21
// currLong is greater than sum of previous 2 numbers23
} else if (sum > currLong) {24
// currLong is smaller than sum of previous 2 numbers28
// currLong == sum of previous 2 numbers29
if (backtrack(num, i + 1, currLong + prev, currLong, length + 1) == true) {