<p>Given an integer <code>n</code>, return all the numbers in the range <code>[1, n]</code> sorted in lexicographical order.</p>

<p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and uses <code>O(1)</code> extra space.&nbsp;</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> n = 13
<strong>Output:</strong> [1,10,11,12,13,2,3,4,5,6,7,8,9]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> n = 2
<strong>Output:</strong> [1,2]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
</ul>

class Solution {
public:

    vector<int> ans;

    void dfs(int n, string s){
        if(!s.empty()){
            if(stol(s)<=n){
                ans.push_back(stoi(s));
            }
            else return;
        }
        int i = 0;
        if(s.length()==0)i=1;
        for(int j=i;j<=9;j++){
            s.push_back(j+'0');
            dfs(n,s);
            s.pop_back();
        }
    }

    vector<int> lexicalOrder(int n) {
        dfs(n,"");
        return ans;
    }
};
15324