当前位置:网站首页>Leetcode 386. Number of dictionary rows
Leetcode 386. Number of dictionary rows
2022-04-22 01:53:00 【Die in a trap】
386、 Dictionary order number
1) Title Description
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]
Tips :
1 <= n <= 5 * 10^4
2) analysis
Depth-first search .
startfrom1Start , Add a zero after , ifstart*10<=n, showsstart*10Is the next number . continue ;- if
start*10>n, So ifstart%10==9perhapsstart+1>n, Indicates that the current bit search is complete , Returns the previous digit plus one .
3)C++ Code
class Solution {
public:
vector<int> lexicalOrder(int n) {
vector<int> res;
int satrt=1;
for(int i=0;i<n;i++){
res.push_back(satrt);
if(satrt*10<=n){
satrt*=10;
}
else{
while(satrt%10==9||satrt+1>n){
satrt/=10;
}
satrt++;
}
}
return res;
}
};
版权声明
本文为[Die in a trap]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211531452708.html
边栏推荐
- 树、二叉树和森林的转换
- [programming question] clear random number
- [acwing] week 8
- DVWA series - XSS (cross site script injection (reflective, storage, DOM))
- Oracle installation server
- B站充电|单周充电超1500,银发UP主成B站新黑马?
- Ali Daniel collates, Android interview multiple-choice questions
- Hexadecimal conversion
- How to set the TS slice file size for the new kernel version of easycvr?
- Advanced file IO of system programming (12) -- blocking and non blocking reading
猜你喜欢
Ali Daniel collates, Android interview multiple-choice questions

Cereal Mall - learning notes - Basic chapter - goods and services 1 (p45-p58)

Phy6222 domestic low power multi protocol Bluetooth SOC chip

Leetcode2016. Maximum difference between incremental elements

WebRTC视频无法播放,如何在EasyCVR添加UDP打洞程序?

Vscode failed to use SSH Remote Connection

Fundamentals lecture 30 Lecture 9 geometric application of univariate function integration

分布式事务解决方案

The difference between B tree and B + tree

Common evaluation indexes of medical image segmentation
随机推荐
Unity3d RPG role playing game source code (top and bottom) --- source code analysis ----- 01 ------ protagonist character animation
718. Longest repeating subarray
B树和B+树的区别
Bluemoon target penetration test
阿里大牛教你自己写Android第三方库,2021最新阿里Android面试流程
Network protocol analysis summary
最长公共子串
[编程题]构造回文
C# 从数据库读取数据, 导出到CSV
[programming question] what is the highest score
新内核版本的EasyCVR如何设置ts切片文件大小?
Leetcode1470. 重新排列数组
[ing] matlab Toolkit - detailed explanation of dynamic copula toolbox 3.0
Cereal Mall - learning notes - Basic chapter - goods and services 1 (p45-p58)
Hemisphere - lynx target penetration test
Alibaba Android development specification, senior programmer interview questions
[programming question] water bottle
Alibaba Daniel teaches you to write your own Android third-party library. The latest Alibaba Android interview process in 2021
QT程序打包成一个exe可执行文件
HJ5 进制转换