LeetCode[386] 字典序排数

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;   
    }
};
posted @   星星亮了欸  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App

阅读目录(Content)

此页目录为空

点击右上角即可分享
微信分享提示