aboutsummaryrefslogtreecommitdiff
path: root/lexicographical_numbers.cpp
blob: 55e9fe35cf6887bf86aed8ce463cdf86cc295333 (plain)
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
// dnw
#include "leetcode.h"

class Solution {
public:
  vector<int> ans;
  vector<int> lexicalOrder(int n) {
    ans.resize(n);
    int pos = 0;
    for (int i = 1; i <= 9 && i <= n; i++)
      helper(i, n, pos);
    return ans;
  }

private:
  void helper(int i, int n, int pos) {
    ans[pos++] = i;
    int tmp;
    for (int j = 0; j <= 9; j++) {
      tmp = i * 10 + j;
      if (tmp > n)
        break;
      helper(tmp, n, pos);
    }
  }
};