当前位置:网站首页>leetcode:386. Dictionary order
leetcode:386. Dictionary order
2022-04-21 23:26:00 【Oceanstar's study notes】
Title source
Title Description

title
Dictionary order , seeing the name of a thing one thinks of its function , It must be sorting strings in the order in the dictionary : In fact, it is the size order of the first different characters from left to right , Whose first different character is smaller , Whose dictionary order is smaller . Take a few examples :
(1)“013”<“12”, The first character is a different character ,‘0’<‘1’
(2)“21”<“23”, The second character is a different character ,‘1’<‘3’
(3)“3242432”<“32484343”, The fourth character is a different character ,‘2’<‘8’
Be careful ️, The dictionary order of a string has nothing to do with its length , Don't confuse .


The cross tree traverses first
Train of thought
0
|
-----------------------------------
| | | | | | | | |
1 2 3 4 5 6 7 8 9
| | | | | | | | |
10~19 20~29 30~39 ............
For input 20, The answer is :
[1,10,11,12,13,14,15,16,17,18,19,2,20,3,4,5,6,7,8,9]
It can be seen that , It's actually the tree above The first sequence traversal DFS, Just put greater than 20 The number of ( also 0) Removed .
Train of thought two
take [1, n] All the numbers in the are sorted in dictionary order , It can be found that the numbers with the same prefix are clustered together . such as n=23, Output is [1,10,11,12,13,2,3,4,5,6,7,8,9], The prefix for 1 Of [1,10,11,12,13] Will gather together , In other words, it is actually a pre order traversal process of a cross tree
Train of thought three

take [1, n] The number of is added to the answer in dictionary order , In essence, the number of nodes is n, The form is similar to the multi-level tree of dictionary tree , The root node is 0, It needs to be skipped , So we can start searching from the second layer of the tree .
The value of each node in the tree is the number represented by its search path , And each node has [0, 9] common 10 Child node .
class Solution {
// 1 , 10 , 11 , 12, 13
void helper(int cur, int n, std::vector<int>& res){
if(cur > n){
return;
}
res.push_back(cur);
for (int i = 0; i < 10; ++i) {
helper(cur * 10 + i, n, res);
}
}
public:
vector<int> lexicalOrder(int n) {
std::vector<int> res;
for (int i = 1; i < 10; ++i) {
helper(i, n, res);
}
return res;
}
};


Iterative writing
class Solution {
public:
vector<int> lexicalOrder(int n) {
// *10 Down , +1 towards the right
vector<int> ret(n);
int number = 1;
for (int i = 0; i < n; i++) {
ret[i] = number;
// Normal down
if (number * 10 <= n) {
number *= 10;
} else {
// towards the right , Go back first
while (number % 10 == 9 || number == n) {
// Reached the border
number /= 10; // upper story
}
number++;
}
}
return ret;
}
};

版权声明
本文为[Oceanstar's study notes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212324225338.html
边栏推荐
- MySQL problem solving_ Multi table joint query_ Please take out the corresponding data and output it in ascending order of accuracy.
- 340-Leetcode 有效的字母异位词
- prompt 你到底行不行?
- 87 R k-means,层次聚类,EM聚类的实现
- Technology, products and brand are not problems. For SAIC Audi, these two points may be life and death
- 2022/4/21
- Why don't MySQL use select * as query criteria? (continuously updated)
- 雲原生架構下的微服務選型和演進
- TextView 倾斜属性
- 简约易收录的导航网站源码
猜你喜欢
技术、产品、品牌都不是问题,对上汽奥迪而言,这2点或生死攸关

雲原生架構下的微服務選型和演進

瑞芯微芯片AI部分开发记录 第一节 《PC端环境搭建1》

文件操作和IO

MySQL Chapter 5 addition, deletion, modification and query of MySQL table data

云原生架构下的微服务选型和演进

(7) Ruixin micro rk3568 builderoot adds compiled scripts and binary program files

叹为观止,4款惊喜满满的高质量软件,使用起来倍感舒适

prompt 你到底行不行?

痞子衡嵌入式:聊聊系统看门狗WDOG1在i.MXRT1xxx系统启动中的应用及影响
随机推荐
VOS6. 0 installation and source code command
leetcode:386. 字典序排数
MySQL problem solving_ Multi table joint query_ Please take out the corresponding data and output it in ascending order of accuracy.
golang力扣leetcode 386.字典序排数
2022/4/21
341-Linux 连接数据库
golang力扣leetcode 380.O(1)时间插入、删除和获取随机元素
Developing Cami community system with ThinkPHP
April 22, 2022 Daily: a new face attribute editing framework based on transformer
Go language self-study series | golang type definition and type alias
手机APP游戏/软件/资源下载站/软件盒子源码
(3) Ruixin micro rk3568 SSH replaces dropbear
87 R k-means,层次聚类,EM聚类的实现
Qt自定义控件01 简易计时器
简约易收录的导航网站源码
Textview tilt properties
从零开始自制实现WebServer(十六)---- 学习新工具CMake自动编写MakeFile 分门别类整理源文件心情愉悦
MySQL Chapter 3 basic SQL syntax
2022 electrician (elementary) examination simulated 100 questions and simulated examination
信噪比和信干噪比