forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
47 lines (36 loc) · 957 Bytes
/
main.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
/// Source : https://leetcode.com/problems/lexicographical-numbers/description/
/// Author : liuyubobobo
/// Time : 2017-10-16
#include <iostream>
#include <vector>
using namespace std;
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
vector<int> lexicalOrder(int n) {
vector<int> res;
for(int i = 1 ; i < 10 ; i ++)
if(i <= n)
generateRes(res, i, n);
return res;
}
private:
void generateRes(vector<int>& res, int pre, int n){
res.push_back(pre);
for(int i = 0 ; i < 10 ; i ++)
if(pre*10+i <= n)
generateRes(res, pre*10+i, n);
}
};
int main() {
vector<int> res1 = Solution().lexicalOrder(13);
for(int num: res1)
cout << num << " ";
cout << endl;
vector<int> res2 = Solution().lexicalOrder(100);
for(int num: res2)
cout << num << " ";
cout << endl;
return 0;
}