Jumping Numbers Finder
mediumSave
BacktrackingBreadth-First SearchMath
Given a positive integer n, find all the 'Jumping Numbers' smaller than or equal to n. A number is defined as a 'Jumping Number' if all adjacent digits in it differ by 1. The output should be a sorted list of these numbers.
Example 1
Input
[ 10 ]
Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Explanation. All digits and 10 are jumping numbers less than or equal to 10.
Example 2
Input
[ 15 ]
Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12]
Explanation. All single-digit numbers are jumping numbers. From 10 to 15, only 10 and 12 qualify as jumping numbers.
Example 3
Input
[ 30 ]
Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 21, 23]
Explanation. Numbers like 10, 12, 21, and 23 show the jumping number property where consecutive digits differ by 1.
Follow-up: Can you optimize your solution to run in linear time?
Constraints:
The input number, `n`, will be greater than 0 and less than or equal to 100,000.
- Views
- 2