1007. Numbers With Same Consecutive Differences
My accepted Python solution to LeetCode problem 1007, Numbers With Same Consecutive Differences, running in 4ms.
- Difficulty: Medium
- Python
- Runtime 4ms
- Memory 17.4MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 4ms, memory 17.4MB, accepted 2026-01-02.
class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
if n == 1:
return list(range(10))
result = []
def dfs(num, length):
if length == n:
result.append(num)
return
last_digit = num % 10
next_digits = set()
if last_digit + k <= 9:
next_digits.add(last_digit + k)
if last_digit - k >= 0:
next_digits.add(last_digit - k)
for d in next_digits:
dfs(num * 10 + d, length + 1)
# Start with digits 1-9 (no leading zeros)
for i in range(1, 10):
dfs(i, 1)
return result