Leetcode: 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.
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的更多相关文章
- [LeetCode] Lexicographical Numbers 字典顺序的数字
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 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 ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)
1. 题目名称 Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...
- Lexicographical Numbers
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
- [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 ...
随机推荐
- Shell-bash中特殊字符汇总[转]
转自http://www.linuxidc.com/Linux/2015-08/121217.htm 首先举例一个bash脚本 #!/bin/bash file=$1 files=`find / -n ...
- Progressive enhancement
https://en.wikipedia.org/wiki/Progressive_enhancement Progressive enhancement is a strategy for web ...
- ubuntu日志清理
由于ubuntu日志文件syslog 和 kern.log 时刻在增长,一会儿就使得根目录文件夹不够用了,需使用如下命令清理 sudo -i输入密码echo > /var/log/syslog ...
- Delphi中如何控制其他程序窗体上的窗口控件
回调函数一般是按照调用者的要求定义好参数和返回值的类型,你向调用者提供你的回调函数的入口地址,然后调用者有什么事件发生的时候就可以随时按照你提供的地址调用这个函数通知你,并按照预先规定好的形式传递参数 ...
- HQL之多表查询(一对多和多对多)
一.一对多 以班级Classes和学生Student为例: 回忆sql语句: //内链接,两种方式效果一样,查询的是两边都有的数据 SELECT c.*,s.* FROM classes c,st ...
- WCF TCP 错误代码 10061: 由于目标计算机积极拒绝
表象是连不上服务端,本质原因多种多样,网络硬件问题导致的网络不通.服务本身问题或没有启动.或者防火墙阻隔等等不一而足. 1.ping看服务端能否ping通: 2.telnet ip地址 端口 ,看看是 ...
- Maven实战(三)Eclipse构建Maven项目
1. 安装m2eclipse插件 要用Eclipse构建Maven项目,我们需要先安装meeclipse插件 点击eclipse菜单栏Help->Eclipse Marketplac ...
- 适用于Magento的最合适的.htaccess写法
原作者地址:http://www.ctrol.cn/post/ecommercial/magento/12-05-ctrol-4057.html ########################### ...
- Chrome 文件另存为和打开本地资源时会卡死的问题
一般是第一次可以 第二次以后就会卡死 另存为问题:弹出窗口没有正常弹出实际已经存在 直接按“回车”下载即可 上传时的问题:如果卡死 可以点击“ESC” 取消操作 解决卡死 但是无法上传了 有人知道原因 ...
- GIS 学习及参考站点
地理信息论坛 GIS空间站 GISALL 广东水利厅 flex版的