Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions 386. Lexicographical Numbers
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
vector<int> lexicalOrder(int n) {
priority_queue<string, vector<string>, greater<string>> heap; // Min-heap for lexicographical order

// Push all numbers as strings into the priority queue
for (int i = 1; i <= n; i++) {
heap.push(to_string(i));
}

vector<int> ans;

// Pop elements from the heap, convert them back to integers, and store them in the answer
while (!heap.empty()) {
ans.push_back(stoi(heap.top())); // Convert string back to integer
heap.pop();
}

return ans;
}
};
Loading