Skip to content

Create 386. Lexicographical Numbers #591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2024
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