当前位置:网站首页>Leetcode-386: number of dictionary rows
Leetcode-386: number of dictionary rows
2022-07-22 13:33:00 【Chrysanthemum headed bat】
leetcode-386: Dictionary order number
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]
Example 2:
Input :n = 2
Output :[1,2]
Problem solving
Method 1 : Dictionary tree + The former sequence traversal
class Trie{
public:
bool isEnd=false;
vector<Trie*> next=vector<Trie*>(10,nullptr);
};
class Solution {
public:
Trie* trie=new Trie();
vector<int> res;
vector<int> lexicalOrder(int n) {
for(int i=1;i<=n;i++){
insert(to_string(i));
}
traversal(trie,0);
return res;
}
// Pre order traversal of dictionary tree
void traversal(Trie* root,int path){
if(root->isEnd){
res.push_back(path);
}
for(int i=0;i<10;i++){
if(root->next[i]){
traversal(root->next[i],path*10+i);
}
}
}
// Dictionary tree insert node
void insert(string&& s){
Trie* node=trie;
for(char c:s){
if(node->next[c-'0']==nullptr){
node->next[c-'0']=new Trie();
}
node=node->next[c-'0'];
}
node->isEnd=true;
}
};
Method 2 : iteration
class Solution {
public:
vector<int> lexicalOrder(int n) {
vector<int> res(n);
int num=1;
for(int i=0;i<n;i++){
res[i]=num;
if(num*10<=n){
num*=10;
}else{
while(num%10==9||num+1>n){
num/=10;
}
num++;
}
}
return res;
}
};
边栏推荐
- 人体体重秤体脂秤方案PCBA设计
- Leetcode-140: word splitting II
- leetcode-1260:二维网格迁移
- Leetcode-6117: the latest time to take the bus
- [QNX Hypervisor 2.2用户手册]8.7 虚拟I/O(VIRTIO)
- QT creator shortcut keys, with shortcut key configuration method
- Leetcode-zj-future04: store commodity allocation
- 2022.7.21-----leetcode. eight hundred and fourteen
- Supply chain collaborative management system of pharmaceutical machinery industry: full link digital coverage to realize the visualization of industrial supply chain
- 直播回顾| Apache Pulsar Meetup 精彩回放(含 PPT 下载)
猜你喜欢
One bite of Stream(9)
C语言动态内存管理
如何安装mysql
leetcode-6112:装满杯子需要的最短总时长
Service (LB) and managed pod
JVM的运行原理
How to use call, bind and apply correctly
"Double business success classic" is newly upgraded and launched! Hot summer new products, waiting for a long time to come!
Elephant Swap的LaaS方案优势分析,致eToken表现强势
How to install MySQL
随机推荐
ApacheCon Asia 2022 开启报名:Pulsar 技术议题重磅亮相
Deep analysis of JVM object creation and memory allocation of JVM (9)
Congratulations on the successful convening of the product innovation management forum of innovation empowerment on July 16
轮子五:QCustomPlot常用类
Leetcode-zj-future03: location of express transfer station
[PostgreSQL 15] PostgreSQL 15 improves unique and null
service(lb) 和管理的pod
leetcode-zj-future04:门店商品调配
JVM类加载和垃圾回收
224. Basic calculator - recursive method
Chart drawing summary
leetcode-745:前缀和后缀搜索
Interview question 05.07 Pairing exchange
数据库扩容也可以如此丝滑,MySQL千亿级数据生产环境扩容实战
(Applied intelligence-2022) transgait: gait recognition and ensemble transformer based on multimodality
leetcode-1260:二维网格迁移
win10如何设置锁屏后不熄屏
Leetcode-zj-future04: store commodity allocation
Robin Lee: driverless driving is the greatest innovation of human value creation
Elemen when clicking, modify the playback index of the walking lantern