Skip to content

Practice Lexicographical Numbers

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#386
LeetCode Problem

Lexicographical Numbers

Problem Statement

Given an integer n, return all the numbers in the range [1, n] sorted in lexicographical order. You must write an algorithm that runs in O(n) time and uses O(1) extra space.

Source: Lexicographical Numbers on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 13
Output
[1,10,11,12,13,2,3,4,5,6,7,8,9]

# Example 2

Input
n = 2
Output
[1,2]

Constraints

1 <= n <= 5 * 10^4

Related LeetCode Problems

Practice more problems similar to Lexicographical Numbers.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.