bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1215. Stepping Numbers

1215. Stepping Numbers

Leetcode Solutions

BFS Approach for Finding Stepping Numbers

  1. Initialize an empty list res to store the stepping numbers within the range.
  2. If low is 0, append it to res since 0 is a stepping number.
  3. Initialize a queue and add the digits 1-9 to it.
  4. While the queue is not empty: a. Dequeue the front number cur from the queue. b. If cur is within the range [low, high], append it to res. c. Get the last digit of cur. d. If the last digit is not 0, enqueue cur * 10 + last - 1. e. If the last digit is not 9, enqueue cur * 10 + last + 1.
  5. Return the sorted list res.
UML Thumbnail

DFS Approach for Finding Stepping Numbers

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...