386. Lexicographical Numbers 把1--n按字典序排序
Posted liuweimingcprogram
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了386. Lexicographical Numbers 把1--n按字典序排序相关的知识,希望对你有一定的参考价值。
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按字典序排序的主要内容,如果未能解决你的问题,请参考以下文章
386. Lexicographical Numbers 把1--n按字典序排序
(Java) LeetCode 386. Lexicographical Numbers —— 字典序排数
386. Lexicographical Numbers 输出1到n之间按lexico排列的数字序列
Swift Playground 错误:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)