Short C++ recursive solution


  • 0
    class Solution {
    private:
        void gen(int& n, int start, int base, vector<int>& ans) {
            for (int i = start; i < 10; i++) {
                int cur = base + i;
                if (cur > n) { return; }
                ans.push_back(cur);
                gen(n, 0, cur * 10, ans);
            }
        }
        
    public:
        vector<int> lexicalOrder(int n) {
            vector<int> ans;
            gen(n, 1, 0, ans);
            return ans;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.