当前位置:网站首页>Leetcode-386 dictionary order
Leetcode-386 dictionary order
2022-04-22 13:48:00 【Cc824 constant】
subject Give you an integer n , Returns the range in dictionary order [1, n] All integers in .
You have to design a time complexity of O(n) And the use of O(1) Extra space algorithm .
Example 1:
Input :n = 13 Output :[1,10,11,12,13,2,3,4,5,6,7,8,9]
If time complexity and space complexity are not considered, brainless operation can be used
The first 1-n Convert integer to string type , Then sort and add to list in
Code :
class Solution {
public List<Integer> lexicalOrder(int n) {
String[] str = new String[n];
for(int i=0;i<n;i++)
str[i]=String.valueOf(i+1); // First convert the integer to string type
Arrays.sort(str); // Sort by dictionary order
List<Integer> list = new ArrayList<>();
for(int i = 0;i<n;i++)
list.add(Integer.valueOf(str[i]));
return list;
}
}
If time and space complexity are taken into account
To use DFS ( This algorithm has not been mastered yet )
recursive dfs
Multi order tree traversal similar to dictionary tree , The root node is 0, To be skipped , From the second floor 1-9 Start
Every node has 0-9 10 Nodes Here's the picture

class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> lexicalOrder(int n) {
for(int i=1;i<=9;i++)
dfs(i,n);
return list;
}
public void dfs(int cur,int limit){
if(cur > limit)
return;
list.add(cur);
for(int i=0;i<=9;i++)
dfs(cur*10+i,limit); // Start from left to right
}
}
Although the above recursion is convenient , But because recursion uses stacks , Spatial complexity is not O(1) All improvements
Use dfs iteration In accordance with the spatial complexity O(1)
class Solution {
public List<Integer> lexicalOrder(int n) {
List<Integer> list = new ArrayList<>();
int number=1;
for(int i=0;i<n;i++){
list.add(number);
if(number*10<=n)
number=number*10;
else{
while(number%10==9||number+1>n) // At the end of each node
number=number/10; // If number=13 Next number=1
number++; //1+1=2 Start looking for 2 Child nodes of
}
}
return list;
}
}
版权声明
本文为[Cc824 constant]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221347257545.html
边栏推荐
- Understand efficientnet
- QT explorer and Use of QRC file
- Good collection (1)
- CDF全球调查:软件交付性能停滞不前
- OceanBase的超级用户 - 系统租户sys
- 树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
- Detailed explanation of heap sorting (C language)
- Chapitre sur la promotion précise des investissements dans la gestion de l'exploitation numérique des parcs industriels
- Originally, this is the correct posture for developers to open world book day!
- EnvironmentPostProcessor怎么做单元测试?阿里P7解答
猜你喜欢

智汀如何连接小米智能音箱?

Fizz enterprise microservice Gateway - service choreography, offering a big killer to end the BFF layer

那些年我们一起优化的SQL

Go down and continue to go down. Xiaohui's fund has lost 640000...

机器越“智能”,数据标注员越容易被淘汰?丨曼孚科技
BAIC Foton, Sinopec and light engineering Internet of things set up Sinopec to sell hydrogen energy (Beijing)

OceanBase的超级用户 - 系统租户sys

產業園區數字化運營管理之“精准招商”篇

线程池中的阻塞队列有哪几种?

Wechat applet adds data to the database
随机推荐
What saved me 60% of my coding time? Use MBG
Digital twin: how to support the industrial transformation of a trillion market?
树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
"Enterprise service" of digital operation and management of Industrial Park
iMeta: 整合宏组学重新认识生命和环境科学
实现全链路重塑!从思维走向实践,数字化转型 IT 经营的成功路径
苏小红C语言程序设计第四、五章知识总结
How to perform multi table associated query on Citrix SQL data
Genesis创意漫画之【稳定通证】
美女同事的烦恼:如何配置 Apache SkyWalking 告警?
在珠江期货办理开户安全吗?
路由基础之OSPF基本配置
MySQL DNS解析和主机缓存
C 7.0 use underline to ignore variables used
七年谋“一剑”,站在边缘云的风口,如何加速企业数字化转型?
翻译翻译什么是适配器模式?
页面嵌套的方法
500 Internal Server Error supplement
Word length and data type
Harbor V2. 5 Mise à jour, quelles fonctions ont été ajoutées?