用DFS来做,先弄开头是1的,再弄开头是1的里面开头是1的,再开头是1的里面开头是1的里的开头是1的,再。。。

是吧……

比N大了BREAK就行。

注意第一个循环是1-9,往后的循环是0-9。

public class Solution
{
public List<Integer> lexicalOrder(int n)
{
List<Integer> res = new ArrayList<>();
for(int i = 1; i < 10; i++)
{
helper(i,res,n);
} return res;
} public void helper(int temp, List<Integer> res, int n)
{
if(temp > n) return;
else
{
res.add(temp);
for(int i = 0; i < 10; i++)
{
if(temp*10 + i > n) break;
else
{
helper(temp*10 + i,res,n);
}
}
}
}
}

386. Lexicographical Numbers的更多相关文章

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

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

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

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

  3. LeetCode - 386. Lexicographical Numbers

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

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

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

  5. 386 Lexicographical Numbers 字典序排数

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

  6. [LeetCode] Lexicographical Numbers 字典顺序的数字

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

  7. Leetcode: Lexicographical Numbers

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

  8. Lexicographical Numbers

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

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

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

随机推荐

  1. 类库探源——System.ValueType

    一.MSDN描述 ValueType 类:提供值类型的基类 命名空间: System 程序集:   mscorlib.dll 继承关系: 值类型包括:字符.整数.浮点.布尔.枚举.结构(其实字符.整数 ...

  2. 菜鸟日记之 java中的集合框架

    java中的集合框架图 如图所示:java中的集合分为两种Collection和Map两种接口 可分为Collection是单列集合和Map的双列集合 Collection单列集合:继承了Iterat ...

  3. .NET开源工程推荐(Accord,AForge,Emgu CV)

         本人用C#开发了一些项目,下面的开源工程给了我很大的帮助——详细的源代码介绍加丰富的实例运用,是非常不错的学习资源,分享给大家,同时附上我的相关开发项目.    Accord.NET The ...

  4. js 通过function来定义函数

    什么是函数: 函数是完成某一功能的代码段. 函数是可重复执行的代码段. 函数方便管理和维护. 自定义一个函数: 通过function关键字来定义一个函数. 语法:   function 函数名称([可 ...

  5. Git版本控制工具使用:Error pulling origin: error: Your local changes to the following files would be overwritten by merge

    摘自: CSDN 逆觞 git在pull时,出现这种错误的时候,可能很多人进进行stash,相关stash的请看:Error pulling origin: error: Your local cha ...

  6. 2016022606 - redis事务

    Redis事务 Redis事务让一组命令在单个步骤执行.事务中有两个属性,说明如下: 1.在一个事务中的所有命令按顺序执行作为单个隔离操作.通过另一个客户端发出的请求在Redis的事务的过程中执行,这 ...

  7. 练习2 I题 - 水仙花数

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 春天是 ...

  8. eclipse/myeclipse选中编辑区域文件,Package Explorer定位文件所在项目及目录

    eclipse/myeclipse选中编辑区域文件,Package Explorer定位文件所在项目及目录 1. 打开Package Explorer(若没有,可以按照如下路径点击: Window菜单 ...

  9. [BZOJ 2127] happiness 【最小割】

    题目链接:BZOJ - 2127 题目分析 首先,每个人要么学文科,要么学理科,所以可以想到是一个最小割模型. 我们就确定一个人如果和 S 相连就是学文,如果和 T 相连就是学理. 那么我们再来确定建 ...

  10. [BZOJ 2186] [Sdoi2008] 沙拉公主的困惑 【欧拉函数】

    题目链接:BZOJ - 2186 题目分析 题目要求出 [1, n!] 中有多少数与 m! 互质.(m <= n) 那么在 [1, m!] 中有 phi(m!) 个数与 m! 互质,如果一个数 ...