码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-08-19 21:54:35      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:return   字典序   graphic   while   pre   ref   com   ber   blank   

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按字典序排序

标签:return   字典序   graphic   while   pre   ref   com   ber   blank   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!