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

Leetcode Problem 386. Lexicographical Numbers

386. Lexicographical Numbers

Leetcode Solutions

Iterative Depth-First Search (DFS)

  1. Initialize an empty list result to store the lexicographically ordered numbers.
  2. Start with current number as 1.
  3. Use a loop to iterate until you have added n numbers to the result list.
  4. Add the current number to the result list.
  5. If current * 10 is less than or equal to n, multiply current by 10.
  6. Else, if current is not the last number in its group (i.e., its last digit is not 9) and current + 1 is less than or equal to n, increment current by 1.
  7. Else, divide current by 10 until the last digit is not 9, then increment current by 1.
  8. Repeat steps 4-7 until the result list contains n numbers.
UML Thumbnail

Recursive Depth-First Search (DFS)

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...