diff --git a/386. Lexicographical Numbers b/386. Lexicographical Numbers new file mode 100644 index 0000000..1166383 --- /dev/null +++ b/386. Lexicographical Numbers @@ -0,0 +1,21 @@ +class Solution { +public: + vector lexicalOrder(int n) { + priority_queue, greater> 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 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; + } +};