Skip to content

Commit 4c36258

Browse files
committed
feat(LeetCode): Solve leetcode/leetcoding_challenge/2025/jun2025/week2/lexicographical_numbers.cpp
1 parent 6bc6e00 commit 4c36258

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <algorithm>
2+
#include <vector>
3+
#include <queue>
4+
#include <set>
5+
#include <limits>
6+
#include <map>
7+
#include <unordered_set>
8+
#include <unordered_map>
9+
#include <iterator>
10+
#include <sstream>
11+
#include <iostream> // includes cin to read from stdin and cout to write to stdout
12+
using namespace std; // since cin and cout are both in namespace std, this saves some text
13+
14+
class Solution {
15+
public:
16+
vector<int> lexicalOrder(int n) {
17+
vector<int> answer;
18+
19+
vector<int> stack;
20+
stack.push_back(1);
21+
22+
// in-order
23+
while (stack.size() > 0) {
24+
auto v = stack.back();
25+
stack.pop_back();
26+
27+
if (v > n) {
28+
continue;
29+
}
30+
31+
answer.push_back(v);
32+
33+
if (v % 10 < 9) {
34+
stack.push_back(v + 1);
35+
}
36+
stack.push_back(v * 10);
37+
}
38+
39+
return answer;
40+
}
41+
};
42+
43+
class WASolution {
44+
public:
45+
vector<int> lexicalOrder(int n) {
46+
vector<int> answer;
47+
48+
deque<int> q;
49+
q.push_back(1);
50+
51+
// in-order
52+
while (q.size() > 0) {
53+
auto v = q.front();
54+
q.pop_front();
55+
56+
if (v > n) {
57+
continue;
58+
}
59+
60+
answer.push_back(v);
61+
62+
q.push_back(v * 10);
63+
q.push_back(v + 1);
64+
}
65+
66+
return answer;
67+
}
68+
};

0 commit comments

Comments
 (0)