【LeetCode】386. Lexicographical Numbers 解题报告(Python)
【LeetCode】386. Lexicographical Numbers 解题报告(Python)
标签(空格分隔): LeetCode
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/
题目地址:https://leetcode.com/problems/lexicographical-numbers/description/
题目描述:
Given an integer n, return 1 - n in lexicographical order.
For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9].
Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000.
题目大意
给了一个数字n,找出1~n的所有数字的字典序排列。
解题方法
题目中说了输入可能是5百万,那么只能用O(N)的时间复杂度。
这个题的思路我是参考的Lexicographical Numbers 字典顺序的数字,不太好想。
- 如果curr乘以10小于等于n,那么下个数字应该是curr末尾补0;
- 如果curr已经到达了n,那么说明各位数字已经到头了,应该变化十位数字了,所以除以10,再加一。这时可能会出现恰好进位的情况,而字典序可能是从末尾没有0的数字开始的,所以要把末尾的0去掉。
比如n=300
时,会有这个队列1,10,100,101,102...198,199,2,20,200,201...299,3,30,300
代码如下:
class Solution:
def lexicalOrder(self, n):
"""
:type n: int
:rtype: List[int]
"""
cur = 1
ans = []
for i in range(n):
ans.append(cur)
if cur * 10 <= n:
cur *= 10
else:
if cur >= n:
cur //= 10
cur += 1
while cur % 10 == 0:
cur //= 10
return ans
日期
2018 年 8 月 17 日 ———— 别人七夕,我在刷题。。
【LeetCode】386. Lexicographical Numbers 解题报告(Python)的更多相关文章
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- LeetCode - 386. Lexicographical Numbers
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【九度OJ】题目1442:A sequence of numbers 解题报告
[九度OJ]题目1442:A sequence of numbers 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1442 ...
随机推荐
- grep -r
今晚改脚本 我发现了一个很有趣的事情,一共56个配置文件 1 # 注意:对一些参数一致的多个文件可以用此方法 2 # grep -r 查找文件内容,其中PARALLEL=2就是我要替换的内容 3 4 ...
- c#跳转
Response.Redirect(EditUrl("MEUID", lblMEUID.Text, "Page2", "PageOneMK" ...
- 1 — 第一个springboot
1.什么是springboot? 老规矩:百度百科一下 2.对springboot快速上手 1).第一种方式:通过官网来创建springboot项目 ---- 了解即可 这里面的创建方式不做过多说明, ...
- HDFS01 概述
HDFS 概述 目录 HDFS 概述 HDFS的产生背景和定义 HDFS产生背景 HDFS定义 优缺点 优点 缺点 组成 NameNode DataNode Secondary NameNode(2n ...
- day10 ajax的基本使用
day10 ajax的基本使用 今日内容 字段参数之choices(重要) 多对多的三种创建方式 MTV与MVC理论 ajax语法结构(固定的) 请求参数contentType ajax如何传文件及j ...
- Learning Spark中文版--第三章--RDD编程(1)
本章介绍了Spark用于数据处理的核心抽象概念,具有弹性的分布式数据集(RDD).一个RDD仅仅是一个分布式的元素集合.在Spark中,所有工作都表示为创建新的RDDs.转换现有的RDD,或者调 ...
- 【leetcode】563. Binary Tree Tilt
Given the root of a binary tree, return the sum of every tree node's tilt. The tilt of a tree node i ...
- ubantu上编辑windows程序
命令简记 cd $GOROOT/src cp -r $GOROOT /root/go1.4 CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.bash 操作 ...
- final&static
final 1.final修饰类,那么该类不能有子类,那么也就没有子类重写父类的方法,也就没有多态 2.final修饰成员变量,那么成员变量要么显式赋值(用第一种),要么在构造方法中赋值 无论哪一种, ...
- Can we call an undeclared function in C++?
Calling an undeclared function is poor style in C (See this) and illegal in C++. So is passing argum ...