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.

Solution 1:

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) {
ArrayList<Integer> res = new ArrayList<Integer>();
for (int i=1; i<=9; i++) {
helper(res, i, n);
}
return res;
} public void helper(ArrayList<Integer> res, int cur, int n) {
if (cur > n) return;
res.add(cur);
for (int i=0; i<=9; i++) {
helper(res, cur*10+i, n);
}
}
}

Solution 2:

O(N) time, O(1) space

The basic idea is to find the next number to add.
Take 45 for example: if the current number is 45, the next one will be 450 (450 == 45 * 10)(if 450 <= n), or 46 (46 == 45 + 1) (if 46 <= n) or 5 (5 == 45 / 10 + 1)(5 is less than 45 so it is for sure less than n).
We should also consider n = 600, and the current number = 499, the next number is 5 because there are all "9"s after "4" in "499" so we should divide 499 by 10 until the last digit is not "9".

Note: 第二、三种情况不能合并的原因是:不一定是因为最后一位是9才需要/10,有可能是因为curr+1>n

 public List<Integer> lexicalOrder(int n) {
List<Integer> list = new ArrayList<>(n);
int curr = 1;
for (int i = 1; i <= n; i++) {
list.add(curr);
if (curr * 10 <= n) {
curr *= 10;
} else if (curr % 10 != 9 && curr + 1 <= n) {
curr++;
} else {
while ((curr / 10) % 10 == 9) {
curr /= 10;
}
curr = curr / 10 + 1;
}
}
return list;
}

Leetcode: Lexicographical Numbers的更多相关文章

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

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

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

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

  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. Leetcode算法比赛---- Lexicographical Numbers

    问题描述 Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10 ...

  5. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  6. [LeetCode] Consecutive Numbers 连续的数字

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

  7. [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)

    1. 题目名称   Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...

  8. Lexicographical Numbers

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

  9. [LeetCode] 902. Numbers At Most N Given Digit Set 最大为 N 的数字组合

    We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}.  (Not ...

随机推荐

  1. Class 实现IDisposing方法

    public class MyResourceHog : IDisposable  {   // 已经被处理过的标记   private bool _alreadyDisposed = false;  ...

  2. regardless of how many processors are devoted to a parallelized execution of this program

    https://en.wikipedia.org/wiki/Amdah's_law Amdahl's law is often used in parallel computing to predic ...

  3. ubuntu挂载其他分区到/home下,将当前分区内容替换

    有时候,我们装系统时,可能因为没注意,把某一个分区分小了,导致到最后,我们的那个盘容不下了, 这时,面临的两个选择就是:要么卸载一些软件,要么重新分区,重装系统,其实,还可以这样,去把其他 多余的盘分 ...

  4. Java Socket Option

    选项 public final static int TCP_NODELAY = 0x0001; public final static int SO_REUSEADDR = 0x04; public ...

  5. zepto源码--init--学习笔记

    先展示init函数,由于笔记本屏幕太小,删掉了部分源码注释,才能在一屏内截图. 当我们调用$()的时候,便会直接调用zepto.init()生成zepto对象,跟jquery生成jquery对象类似. ...

  6. Wordpress引入多说插件

    多说为Wordpress制作了插件,按照以下步骤,快速让你的网站迅速使用多说! 1.下载插件:https://wordpress.org/plugins/duoshuo/ 或在WordPress后台“ ...

  7. 让android webView使用系统默认浏览器内核直接解析,不弹出选择浏览器选项

    遇到一个需求,要求浏览网页的页面不去启动其他的浏览器,全部在自身的应用中. 解决方法 webview.setWebViewClient(new WebViewClient() { @Override ...

  8. jquery.autocomplete.js 插件的自定义搜索规则

    这二天开始用jquery.autocomplete这个自动完成插件.功能基本比较强大,但自己在实际需求中发现还是有一处不足!问题是这样:当我定义了一个本地数据JS文件时,格式为JSON式的数组.如下: ...

  9. LightOj1418 - Trees on My Island(Pick定理)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1418 题意:给你多边形中的顶点,n个点按顺时针或逆时针方向给出,然后求出多边形内部有多 ...

  10. github Mac端的使用案例

    1. 本地有一个仓库,是和网页版的github连接在一起的,平时用Terminal来控制的,怎么放在github的客户端呢? 解决办法: 1.1 点击左上角的+ 号,在弹出框中选择Add,然后choo ...