跳至主要内容

386. 字典序排数

class Solution {
public:
    vector<int> lexicalOrder(int n) {
        vector<int> ret;
        lexicalOrder2(n, 0, ret);
        return ret;
    }
   
    void lexicalOrder2(int n, int pre, vector<int> & ret)
    {
        if (ret.size() >= n)
        {
            return;
        }
       
        for (int i = (pre==0?1:0);i<=9;i++)
        {
            if ((long long)pre*10+i <= (long long)n)
            {
                ret.push_back(pre*10+i);
                lexicalOrder2(n, pre*10+i, ret);
            }
        }
    }
};

评论

此博客中的热门博文