comments | difficulty | edit_url | tags | ||
---|---|---|---|---|---|
true |
Medium |
|
Given two integers n and k, return an array of all the integers of length n
where the difference between every two consecutive digits is k
. You may return the answer in any order.
Note that the integers should not have leading zeros. Integers as 02
and 043
are not allowed.
Example 1:
Input: n = 3, k = 7 Output: [181,292,707,818,929] Explanation: Note that 070 is not a valid number, because it has leading zeroes.
Example 2:
Input: n = 2, k = 1 Output: [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98]
Constraints:
2 <= n <= 9
0 <= k <= 9
We can enumerate the first digit of all numbers of length
Specifically, we first define a boundary value
The time complexity is
class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
def dfs(x: int):
if x >= boundary:
ans.append(x)
return
last = x % 10
if last + k <= 9:
dfs(x * 10 + last + k)
if last - k >= 0 and k != 0:
dfs(x * 10 + last - k)
ans = []
boundary = 10 ** (n - 1)
for i in range(1, 10):
dfs(i)
return ans
class Solution {
private List<Integer> ans = new ArrayList<>();
private int boundary;
private int k;
public int[] numsSameConsecDiff(int n, int k) {
this.k = k;
boundary = (int) Math.pow(10, n - 1);
for (int i = 1; i < 10; ++i) {
dfs(i);
}
return ans.stream().mapToInt(i -> i).toArray();
}
private void dfs(int x) {
if (x >= boundary) {
ans.add(x);
return;
}
int last = x % 10;
if (last + k < 10) {
dfs(x * 10 + last + k);
}
if (k != 0 && last - k >= 0) {
dfs(x * 10 + last - k);
}
}
}
class Solution {
public:
vector<int> numsSameConsecDiff(int n, int k) {
vector<int> ans;
int boundary = pow(10, n - 1);
auto dfs = [&](auto&& dfs, int x) {
if (x >= boundary) {
ans.push_back(x);
return;
}
int last = x % 10;
if (last + k < 10) {
dfs(dfs, x * 10 + last + k);
}
if (k != 0 && last - k >= 0) {
dfs(dfs, x * 10 + last - k);
}
};
for (int i = 1; i < 10; ++i) {
dfs(dfs, i);
}
return ans;
}
};
func numsSameConsecDiff(n int, k int) (ans []int) {
bounary := int(math.Pow10(n - 1))
var dfs func(int)
dfs = func(x int) {
if x >= bounary {
ans = append(ans, x)
return
}
last := x % 10
if last+k < 10 {
dfs(x*10 + last + k)
}
if k > 0 && last-k >= 0 {
dfs(x*10 + last - k)
}
}
for i := 1; i < 10; i++ {
dfs(i)
}
return
}
function numsSameConsecDiff(n: number, k: number): number[] {
const ans: number[] = [];
const boundary = 10 ** (n - 1);
const dfs = (x: number) => {
if (x >= boundary) {
ans.push(x);
return;
}
const last = x % 10;
if (last + k < 10) {
dfs(x * 10 + last + k);
}
if (k > 0 && last - k >= 0) {
dfs(x * 10 + last - k);
}
};
for (let i = 1; i < 10; i++) {
dfs(i);
}
return ans;
}
/**
* @param {number} n
* @param {number} k
* @return {number[]}
*/
var numsSameConsecDiff = function (n, k) {
const ans = [];
const boundary = 10 ** (n - 1);
const dfs = x => {
if (x >= boundary) {
ans.push(x);
return;
}
const last = x % 10;
if (last + k < 10) {
dfs(x * 10 + last + k);
}
if (k > 0 && last - k >= 0) {
dfs(x * 10 + last - k);
}
};
for (let i = 1; i < 10; i++) {
dfs(i);
}
return ans;
};