Skip to content

Commit 911f8c8

Browse files
authored
Create Numbers With Same Consecutive Differences
1 parent dd18aa5 commit 911f8c8

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
List<String> list = new ArrayList<>();
3+
public int[] numsSameConsecDiff(int N, int K) {
4+
if(N == 1) return new int[]{0,1,2,3,4,5,6,7,8,9};
5+
for(int i = 1; i<10; i++){
6+
addString(i+"", N, K, 1);
7+
}
8+
int[] res = new int[list.size()];
9+
for(int i = 0; i<list.size(); i++){
10+
res[i] = Integer.parseInt(list.get(i));
11+
}
12+
return res;
13+
}
14+
public void addString(String a, int N, int K, int ix){
15+
if(a.length() == N){
16+
list.add(a);
17+
return ;
18+
}
19+
for(int i = 0; i<10; i++){
20+
int prev = Integer.parseInt(a.substring(ix-1, ix));
21+
if(Math.abs(prev - i) == K){
22+
a += i+"";
23+
addString(a, N, K, ix+1);
24+
a = a.substring(0, a.length()-1);
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)