967. Numbers With Same Consecutive Differences (M)
Return all non-negative integers of length n
such that the absolute difference between every two consecutive digits is k
.
Note that every number in the answer must not have leading zeros. For example, 01
has one leading zero and is invalid.
You may return the answer in any order.
Example 1:
Example 2:
Constraints:
2 <= n <= 9
0 <= k <= 9
Solution:
Version 1: DFS
Version 2: BFS
先定下第一位的数字1-9, 再依次BFS 入栈
i.e,(3,4) --> 1, 14, 141, 147, 1414, 1474
Last updated
Was this helpful?