Lexicographical Numbers
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.
方法1:
利用排序,把数字按照lexicographical order 排序。但是最后通不过。
public class Solution {
public List<Integer> lexicalOrder(int n) {
List<Integer> list = new ArrayList<>();
if (n < ) return list;
for (int i = ; i <= n; i++) {
list.add(i);
}
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
return String.valueOf(i1).compareTo(String.valueOf(i2));
}
});
return list;
}
}
方法2:来自leetcode discuss https://discuss.leetcode.com/topic/55377/simple-java-dfs-solution
The idea is pretty simple. If we look at the order we can find out we just keep adding digit from 0 to 9 to every digit and make it a tree. Then we visit every node in pre-order.
1 2 3 ...
/\ /\ /\
10 ...19 20...29 30...39 ....
public class Solution {
public List<Integer> lexicalOrder(int n) {
List<Integer> res = new ArrayList<>();
for (int i = ; i < ; ++i) {
dfs(i, n, res);
}
return res;
}
public void dfs(int cur, int n, List<Integer> res) {
if (cur > n) return;
res.add(cur);
for (int i = ; i < ; ++i) {
dfs( * cur + i, n, res);
}
}
}
Lexicographical Numbers的更多相关文章
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- [LeetCode] Lexicographical Numbers 字典顺序的数字
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
- Leetcode: Lexicographical Numbers
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
- [Swift]LeetCode386. 字典序排数 | Lexicographical Numbers
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
- 386. Lexicographical Numbers 输出1到n之间按lexico排列的数字序列
[抄题]: Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,1 ...
- LeetCode - 386. Lexicographical Numbers
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
- Leetcode算法比赛---- Lexicographical Numbers
问题描述 Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10 ...
- 386. Lexicographical Numbers
用DFS来做,先弄开头是1的,再弄开头是1的里面开头是1的,再开头是1的里面开头是1的里的开头是1的,再... 是吧-- 比N大了BREAK就行. 注意第一个循环是1-9,往后的循环是0-9. pub ...
- 386. Lexicographical Numbers 把1--n按字典序排序
https://leetcode.com/problems/lexicographical-numbers/description/ 前20个是 1, 10, 11, 12, 13, 14, .... ...
随机推荐
- nginx的负载均衡和反响代理配置
4. 负载均衡配置 nginx 的 upstream默认是以轮询的方式实现负载均衡,这种方式中,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 另外 ...
- 【转】wireshark过滤规则
WireShark过滤语法 1.过滤IP,如来源IP或者目标IP等于某个IP 例子:ip.src eq 192.168.1.107 or ip.dst eq 192.168.1.107或者ip.add ...
- HBase Shell输入命令无法删除问题解决技巧
一.引言: HBase shell使用过程中,使用CRT客户端,命令输入后无法删除一直困绕着我,今天终于受不了,几番度娘,谷哥之后,终于有了解决方法,特共享给大家. 二.操作步骤 secureCRT中 ...
- 八大排序方法汇总(选择排序,插入排序-简单插入排序、shell排序,交换排序-冒泡排序、快速排序、堆排序,归并排序,计数排序)
2013-08-22 14:55:33 八大排序方法汇总(选择排序-简单选择排序.堆排序,插入排序-简单插入排序.shell排序,交换排序-冒泡排序.快速排序,归并排序,计数排序). 插入排序还可以和 ...
- strlen的C/C+++实现
2013-07-05 11:36:05 小结: 本函数给出了几种strlen的实现,有ugly implementation,也有good implementation.并参考标准库中的impleme ...
- poj 1201 Intervals(差分约束)
题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...
- SQL列数据转换为字符串
行列转换,将列数据转换为字符串输出 ) SET @center_JZHW = ( SELECT DISTINCT STUFF( ( SELECT ',' + ce_code FROM ap_cente ...
- FreeMarker中if标签内的判断条件
reeMarker中的<#if>标签除了里面直接判断 boolean 类型的变量外,也可以进行表达式判断,有几个细节记录一下 1. 判断对象是否存在(null) 经常会用到,如果对象 != ...
- bzoj1588,1208,1503
进入splay tree的学习中: 据说splay tree在理论上功能十分强大,好好学: splay首先一定是一棵BST,所以记不得的时候画个图就明白: 首先总结一下splay基本的操作左旋,右旋: ...
- python跟踪脚本进度(类似bash-x)
#详细追踪 python -m trace --trace jin.py #显示调用了哪些函数 python -m trace --trackcalls jin.py