-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path967. Numbers With Same Consecutive Differences.java
36 lines (36 loc) · 1.45 KB
/
967. Numbers With Same Consecutive Differences.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
36
class Solution {
public int[] numsSameConsecDiff(int n, int k) {
List<String> result = new ArrayList<>();
backtrack(result, new StringBuilder(), n, k, true);
int[] res = new int[result.size()];
for(int i=0; i<result.size(); i++){
res[i] = Integer.parseInt(result.get(i));
}
return res;
}
private void backtrack(List<String> result, StringBuilder sb, int n, int k, boolean firstZero){
if(sb.length() == n){
StringBuilder copy = new StringBuilder(sb);
result.add(copy.toString());
return;
}
for(int i=0; i<=9; i++){
if(i == 0 && firstZero) continue;
else{
if(sb.length() == 0){
sb.append(i);
backtrack(result, sb, n, k, false);
sb.deleteCharAt(sb.length()-1);
}
else{
int lastChar = sb.charAt(sb.length()-1) - '0';
if(Math.abs(i - lastChar) == k){
sb.append(i);
backtrack(result, sb, n, k, false);
sb.deleteCharAt(sb.length()-1);
}
}
}
}
}
}