首页 > 其他分享 >LC[386] 字典序排数

LC[386] 字典序排数

时间:2022-11-21 22:57:41浏览次数:82  
标签:排数 LC int 386 ans cur

https://leetcode.cn/problems/lexicographical-numbers/description/

想像成一颗树的遍历
img

AC代码:

class Solution {
public:
    vector<int> lexicalOrder(int n) {
        int cur = 1;
        vector<int> ans(n);
        for (int i = 0; i < n; i++)
        {
            ans[i] = cur;
            if (cur * 10 <= n) {
                cur *= 10;
            }else {
                while (cur == n || cur%10 == 9) {
                    cur /= 10;
                }
                cur++;
            }
        }
        return ans;   
    }
};

标签:排数,LC,int,386,ans,cur
From: https://www.cnblogs.com/StarTwinkle/p/16913616.html

相关文章