967. Numbers With Same Consecutive Differences
Question
Return all non-negative integers of length
n
such that the absolute difference between every two consecutive digits isk
.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.
Solution
回溯,每次传入上一个数字和剩余的位数。
全局变量sum记录加和。
DFS搜索,每次计算下一位的可行数字并递归。
用回溯维护sum的值。
如果剩余位数为1,则将当前的sum加入结果,并清零sum。
Code
1 | class Solution { |
967. Numbers With Same Consecutive Differences
https://xuanhe95.github.io/2022/09/02/967-Numbers-With-Same-Consecutive-Differences/