386. Lexicographical Numbers
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method Explanation
The problem requires generating numbers from 1 to n in lexicographical order, with strict O(n) time and O(1) space complexity constraints. This rules out naive solutions like generating all numbers and sorting them.
Solution
Lexicographical Numbers in Python
class Solution:
def lexicalOrder(self, n: int) -> List[int]:
result = []
current = 1
for _ in range(n):
result.append(current)
# Case 1: Try to go deeper (e.g., from 1 to 10, 12 to 120)
if current * 10 <= n:
current *= 10
else:
# Case 2: Cannot go deeper, move to sibling or uncle
# If current is at the limit n, we must backtrack.
# e.g., n=13, current=13 -> backtrack to 1, then increment to 2
if current >= n:
current //= 10
# Move to the next sibling (e.g., 11 -> 12)
current += 1
# Handle cases like 19 -> 20, which should be 2.
# We strip trailing zeros to find the correct next branch.
while current % 10 == 0:
current //= 10
return result
Lexicographical Numbers in Java
class Solution {
public List<Integer> lexicalOrder(int n) {
List<Integer> result = new ArrayList<>(n);
int current = 1;
for (int i = 0; i < n; i++) {
result.add(current);
// Case 1: Try to go deeper (e.g., from 1 to 10, 12 to 120)
if (current * 10 <= n) {
current *= 10;
} else {
// Case 2: Cannot go deeper, move to sibling or uncle
// If current is at the limit n, we must backtrack.
// e.g., n=13, current=13 -> backtrack to 1, then increment to 2
if (current >= n) {
current /= 10;
}
// Move to the next sibling (e.g., 11 -> 12)
current += 1;
// Handle cases like 19 -> 20, which should be 2.
// We strip trailing zeros to find the correct next branch.
while (current % 10 == 0) {
current /= 10;
}
}
}
return result;
}
}
Lexicographical Numbers in C++
class Solution {
public:
vector<int> lexicalOrder(int n) {
vector<int> result;
result.reserve(n); // Pre-allocate memory to avoid reallocations
int current = 1;
for (int i = 0; i < n; ++i) {
result.push_back(current);
// Case 1: Try to go deeper (e.g., from 1 to 10, 12 to 120)
if (current <= n / 10) { // More robust check to avoid overflow: current * 10 <= n
current *= 10;
} else {
// Case 2: Cannot go deeper, move to sibling or uncle
// If current is at the limit n, we must backtrack.
// e.g., n=13, current=13 -> backtrack to 1, then increment to 2
if (current >= n) {
current /= 10;
}
// Move to the next sibling (e.g., 11 -> 12)
current += 1;
// Handle cases like 19 -> 20, which should be 2.
// We strip trailing zeros to find the correct next branch.
while (current % 10 == 0) {
current /= 10;
}
}
}
return result;
}
};
Lexicographical Numbers in TypeScript
function lexicalOrder(n: number): number[] {
const result: number[] = [];
let current = 1;
for (let i = 0; i < n; i++) {
result.push(current);
// Case 1: Try to go deeper (e.g., from 1 to 10, 12 to 120)
if (current * 10 <= n) {
current *= 10;
} else {
// Case 2: Cannot go deeper, move to sibling or uncle
// If current is at the limit n, we must backtrack.
// e.g., n=13, current=13 -> backtrack to 1, then increment to 2
if (current >= n) {
current = Math.floor(current / 10);
}
// Move to the next sibling (e.g., 11 -> 12)
current += 1;
// Handle cases like 19 -> 20, which should be 2.
// We strip trailing zeros to find the correct next branch.
while (current % 10 === 0) {
current = Math.floor(current / 10);
}
}
}
return result;
}
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.