-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNumbersWithSameConsecutiveDifferences.java
51 lines (44 loc) · 1.36 KB
/
NumbersWithSameConsecutiveDifferences.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.smlnskgmail.jaman.leetcodejava.medium;
import java.util.ArrayList;
import java.util.List;
// https://leetcode.com/problems/numbers-with-same-consecutive-differences/
public class NumbersWithSameConsecutiveDifferences {
private final int n;
private final int k;
public NumbersWithSameConsecutiveDifferences(int n, int k) {
this.n = n;
this.k = k;
}
public int[] solution() {
if (n == 1) {
return new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
}
List<Integer> nums = new ArrayList<Integer>();
for (int num = 1; num < 10; num++) {
dfs(n - 1, num, k, nums);
}
int[] result = new int[nums.size()];
for (int i = 0; i < result.length; i++) {
result[i] = nums.get(i);
}
return result;
}
private void dfs(int n, int num, int k, List<Integer> nums) {
if (n == 0) {
nums.add(num);
return;
}
List<Integer> nextDigits = new ArrayList<>();
int tail = num % 10;
nextDigits.add(tail + k);
if (k != 0) {
nextDigits.add(tail - k);
}
for (int nextDigit : nextDigits) {
if (0 <= nextDigit && nextDigit < 10) {
int newNum = num * 10 + nextDigit;
dfs(n - 1, newNum, k, nums);
}
}
}
}