LeetCode - 386. 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.
按字典序求所给范围数字列表
回溯,dfs
class Solution {
List<Integer> res;
public List<Integer> lexicalOrder(int n) {
res = new ArrayList<>();
for (int i=1; i<10; i++) {
if (i > n)
break;
dfs(n, i);
}
return res;
}
public void dfs(int n, int cur) {
if (cur > n)
return;
res.add(cur);
for (int i=0; i<10; i++) {
int next = cur * 10 + i;
if (next > n)
return;
dfs(n, next);
}
}
}
LeetCode - 386. Lexicographical Numbers的更多相关文章
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 386. Lexicographical Numbers 输出1到n之间按lexico排列的数字序列
[抄题]: Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,1 ...
- 386. Lexicographical Numbers 把1--n按字典序排序
https://leetcode.com/problems/lexicographical-numbers/description/ 前20个是 1, 10, 11, 12, 13, 14, .... ...
- 386 Lexicographical Numbers 字典序排数
给定一个整数 n, 返回从 1 到 n 的字典顺序.例如,给定 n =1 3,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] .请尽可能的优化算法的时间复杂度和空间复杂度. 输入 ...
- 386. Lexicographical Numbers
用DFS来做,先弄开头是1的,再弄开头是1的里面开头是1的,再开头是1的里面开头是1的里的开头是1的,再... 是吧-- 比N大了BREAK就行. 注意第一个循环是1-9,往后的循环是0-9. pub ...
- LeetCode——Find All Numbers Disappeared in an Array
LeetCode--Find All Numbers Disappeared in an Array Question Given an array of integers where 1 ≤ a[i ...
- [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 ...
- Leetcode算法比赛---- Lexicographical Numbers
问题描述 Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10 ...
随机推荐
- SVN clean失败解决方法【转】
原文地址:http://blog.csdn.net/victory08/article/details/42100325/ svn执行clean up后出现提示:svn cleanup failed– ...
- test.cpp:(.text+0xc0): undefined reference to `cv::imread(std::string const&, int)'
opencv报错: test.cpp:(.text+0xc0): undefined reference to `cv::imread(std::string const&, int)' te ...
- Error-MVC:HTTP Error 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效。
ylbtech-Error-MVC:HTTP Error 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 1.返回顶部 1. IIS ...
- oracle11g-linux 归档处理
在使用oracle时突然登录不上去了提示:ORA-00257:archiver error.Connect internal only,until freed. 查遍很多资料,都指向“归档日志空间不足 ...
- How to chain a command after sudo su?
The idea is simple, for example: alias foo='sudo su foo && cd /tmp' However, it does not exe ...
- [Unit Testing] Using Mockito Annotations - @Mock, @InjectMocks, @RunWith
Previously we have seen how to do Unit testing with Mockito; import org.junit.Test; import static or ...
- 后台任务hangfire
Installation¶ There are a couple of packages for Hangfire available on NuGet. To install Hangfire in ...
- O_DIRECT与O_SYNC区别(转)
O_DIRECT和O_SYNC是系统调用open的flag参数.通过指定open的flag参数,以特定的文件描述符打开某一文件. 这两个flag会对写盘的性能有很大的影响,因此对这两个flag做一些详 ...
- ASP.NET MVC 4 简介
介绍 使用Visual Studio 2010开发的话,首先需要安装MVC4,可以参考另一篇文章 进行安装. ASP.NET MVC 运行流程 (Clinet) Http Request 客户端发起 ...
- 【SqlServer】SqlServer的异常处理
在SQLserver数据库中,如果有很多存储过程的时候,我们会使用动态SQL进行存储过程调用存储过程,这时候,很可能在某个环节就出错了,但是出错了我们很难去跟踪到出错的存储过程,此时我们就可以使用异常 ...