给定一个整数 n, 返回从 1 到 n 的字典顺序。
例如,
给定 n =1 3,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] 。
请尽可能的优化算法的时间复杂度和空间复杂度。 输入的数据 n 小于等于 5,000,000。
详见:https://leetcode.com/problems/lexicographical-numbers/description/

C++:

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

参考:https://www.cnblogs.com/grandyang/p/5798275.html

386 Lexicographical Numbers 字典序排数的更多相关文章

  1. Java实现 LeetCode 386 字典序排数

    386. 字典序排数 给定一个整数 n, 返回从 1 到 n 的字典顺序. 例如, 给定 n =1 3,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] . 请尽可能的优化算法的时 ...

  2. LeetCode 386——字典序排数

    1. 题目 2. 解答 2.1 方法一 假设返回 118 以内数的字典顺序,则为 1,10,100,101,102,...,109,11,110,111,112,...,118,12,13,....根 ...

  3. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. [Swift]LeetCode386. 字典序排数 | Lexicographical Numbers

    Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...

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

    https://leetcode.com/problems/lexicographical-numbers/description/ 前20个是 1, 10, 11, 12, 13, 14, .... ...

  6. 386. Lexicographical Numbers 输出1到n之间按lexico排列的数字序列

    [抄题]: Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,1 ...

  7. LeetCode - 386. Lexicographical Numbers

    Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...

  8. 386. Lexicographical Numbers

    用DFS来做,先弄开头是1的,再弄开头是1的里面开头是1的,再开头是1的里面开头是1的里的开头是1的,再... 是吧-- 比N大了BREAK就行. 注意第一个循环是1-9,往后的循环是0-9. pub ...

  9. E - 不容易系列之(4)――考新郎 错排数公式

    国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样的:  首先,给每位新娘打扮得几乎一模一 ...

随机推荐

  1. Thinkphp5.0 的响应方式

    Thinkphp5.0 的响应方式 $res = config('default_return_type'); dump($res);//默认是html //修改为json \think\Config ...

  2. UVA 674_Coin Change

    题意: 给定一个数,求用1,5,10,25,50有多少种组合方式. 分析: 简单计数dp,dp[i][j]表示由前i+1个元素组成j的种数,注意dp[i][0]初始化为1,因为一个元素也不选的方法总是 ...

  3. P1605 迷宫 洛谷

    https://www.luogu.org/problem/show?pid=1605 题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐 ...

  4. 什么是单点登录(SSO)

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 在我实习之前我就已经在看单点登录的是什么了,但是实习 ...

  5. Windows 10+Ubuntu 16.04在MBR分区上安装双系统之后没有Windows 10的启动菜单解决方法

    背景: 硬盘分区方式:MBR 硬盘容量256,Windows 100,Ubuntu 156,其中主分区安装的是Windows,Ubuntu安装在逻辑分区上,文件系统为Ext4,整个Ubuntu就挂载在 ...

  6. 携程Apollo(阿波罗)配置中心把现有项目的配置文件迁移到Apollo

    说明: 1.这个示例应该算是一个静态迁移,也就是说配置更新后要重启应用才能体现更新,目的是展示现有配置的如何迁移. 2.如果要实现更新配置后动态去更新而不重启应用的操作,比如ZK地址和数据库地址这些, ...

  7. JSP的调试

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/debugging.html: 一.使用System.out.println() System.out.p ...

  8. python操作mysql方法和常见问题

    http://www.cnblogs.com/ma6174/archive/2013/02/21/2920126.html 安装mysql模块 sudo easy_install mysql-pyth ...

  9. git remote加入本地库的方法

    方法来自airk: 假设须要将你电脑本地的一个git库(目录)B 加入到另外一个git库(目录) A的 remote里 操作方法例如以下: 先在git仓库B操作: git init --bare 然后 ...

  10. Windows 7 蓝屏代码大全 &amp; 蓝屏全攻略

    关于Windows 7.Vista等系统的蓝屏.之前软媒在Win7之家和Vista之家都有非常多文章讨论过,可是都是筛选的常见的一些问题,今天这个文章是个大全.希望大家看着别头痛.文章收藏下来以后待查 ...