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 ...
随机推荐
- 大话数据结构(十二)java程序——KMP算法及改进的KMP算法实现
1.朴素的模式匹配算法 朴素的模式匹配算法:就是对主串的每个字符作为子串开头,与要连接的字符串进行匹配.对主串做大循环,每个字符开头做T的长度的小循环,直到成功匹配或全部遍历完成为止. 又称BF算法 ...
- Android之Fragment学习笔记①
Android Fragment完全解析,关于碎片你所需知道的一切 一. 什么是FragmentFragment(碎片)就是小型的Activity,它是在Android3.0时出现的.Fragment ...
- Windows与Linux共享文件夹互相访问
[原文] 首先安装并配置软件samba [html] view plain copy sudo yum install samba samba-client vim /etc/samba/smb.c ...
- pointer-events:none;穿透属性
从属性字面上看可以理解为:手势时间无效. 当我们在ios下想复制一段文字是,不知道原因导致一些莫名的怪异现象:总是无法复制文字,却意外的发现无论怎么着复制的始终都是图片时, 当我们在一个半透明遮罩上想 ...
- [LeetCode]题解(python):049-Groups Anagrams
题目来源 https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For ...
- Android笔记:C memory copy
socket通讯问题之一: 在c中按字节发送数据 比如设备1状态(1字节)值(1字节)设备2状态(1字节)值(1字节)....这种格式拆分的问题 在c中可以利用struct的 memory copy ...
- undefined和void
1.undefined undefined在js中并不是关键字/保留字,因此在IE5.5~8中可以对undefined赋值,但是在IE9以上,对其赋值是无效的 <script> var a ...
- 四个很好用的Sql Server 日期函数:DateDiff、DatePart、DateAdd、DateName
我以前查一段时间范围内的数据都是在程序里计算好日期再掉查询语句,现在我用下面的函数.SQL SERVER没有查一季度数据的函数. DateDiff函数: 描述 返回两个日期之间的时间间隔. 语法 Da ...
- 编译mod_jk.so
编译mod_jk.so前,需要先到http://tomcat.apache.org/download-connectors.cgi去下载tomcat-connectors-1.2.41-src.tar ...
- 《JAVA NIO》Channel
3.通道 Channle主要分为两类:File操作对应的FIleChannel和Stream操作对应的socket的3个channe. 1.这3个channel都是抽象类.其具体实现在SPI里面. 2 ...