-
Notifications
You must be signed in to change notification settings - Fork 5
/
LC_386_lexicographicalNumbers.cpp
65 lines (46 loc) · 1.07 KB
/
LC_386_lexicographicalNumbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
https://binarysearch.com/problems/N-Lexicographic-Integers
https://leetcode.com/problems/lexicographical-numbers/
386. Lexicographical Numbers
*/
class Solution {
public:
vector<int> ans;
void find(int x, int N)
{
if(x > N)
return;
ans.push_back(x);
for(int i=0; i<=9; i++)
find(x*10 + i, N);
}
vector<int> lexicalOrder(int n) {
for(int i=1; i<=min(9,n); i++)
find(i, n);
return ans;
}
};
// vector<int> solve(int n) {
// vector<string> s;
// for(int i=1; i<=n; i++)
// s.push_back(to_string(i));
// sort(s.begin(), s.end());
// vector<int> ans;
// for(int i=0; i<n; i++)
// ans.push_back(stoi(s[i]));
// return ans;
// }//end
void find(int x, int N, vector<int>& ans)
{
if(x > N)
return;
ans.push_back(x);
for(int i=0; i<=9; i++)
find(x*10 + i, N, ans);
}
vector<int> solve(int n) {
vector<int> ans;
for(int i=1; i<=9; i++)
find(i, n, ans);
return ans;
}//end