Skip to content

Commit e1019ff

Browse files
authored
Create 386. Lexicographical Numbers
1 parent d9bfde0 commit e1019ff

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

386. Lexicographical Numbers

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
vector<int> lexicalOrder(int n) {
4+
priority_queue<string, vector<string>, greater<string>> heap; // Min-heap for lexicographical order
5+
6+
// Push all numbers as strings into the priority queue
7+
for (int i = 1; i <= n; i++) {
8+
heap.push(to_string(i));
9+
}
10+
11+
vector<int> ans;
12+
13+
// Pop elements from the heap, convert them back to integers, and store them in the answer
14+
while (!heap.empty()) {
15+
ans.push_back(stoi(heap.top())); // Convert string back to integer
16+
heap.pop();
17+
}
18+
19+
return ans;
20+
}
21+
};

0 commit comments

Comments
 (0)