首页 > 编程语言 > 详细

386. Lexicographical Numbers 把1--n按字典序排序

时间:2018-08-19 21:27:28      阅读:189      评论:0      收藏:0      [点我收藏+]

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

 

前20个是

1, 10, 11, 12, 13, 14, .....19 2, 20, 3, 4, 5, 6, ....9

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

 

386. Lexicographical Numbers 把1--n按字典序排序

原文:https://www.cnblogs.com/liuweimingcprogram/p/9502551.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!