当前位置:网站首页>【dfs】386. Dictionary order
【dfs】386. Dictionary order
2022-04-22 20:26:00 【wjh776a68】
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]
Tips :
- 1 <= n <= 5 * 104
answer
Using constant space dfs
link
Explain
class Solution {
public:
vector<int> lexicalOrder(int n) {
// Using stack dfs, Can pass , But it is uncertain whether it meets the space-time complexity of the subject requirements
vector<int> ans;
stack<int> buf;
int curnum = 1;
buf.push(curnum);
while (!buf.empty()) {
curnum = buf.top();
buf.pop();
while (curnum <= n) {
ans.push_back(curnum);
if (curnum % 10 < 9) {
buf.push(curnum+1);
}
curnum *= 10;
}
}
return ans;
}
};
版权声明
本文为[wjh776a68]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222018459619.html
边栏推荐
- Matlab learning notes - calculate eigenvectors and manually execute PCA
- Better implementation ideas based on MySQL in some scenarios (continuous update)
- 2022年土建施工员题库精准小题库建设厅施工员
- Screen adaptation of Android interview questions + Aidl
- 网站:fakeimg.pl(文字 --> 图片)
- 普通函数做友元(用举例了解友函数)
- 如何让机器人更像“人”,让slam更灵活?
- 字符数组与字符串:删除字符串所有空格。 (10 分)编写一个函数,用来删除字符串中的所有空格。
- 对话木瓜移动创始人沈思 l 从硅谷到北京
- D trigger in FPGA
猜你喜欢
随机推荐
MySQL 第8章 数据库约束
Redis cluster 6.2.6
CmsEasy7.6.3.2逻辑漏洞
Connection method of Jincang database kingbasees
华为机试题——HJ72 百钱买百鸡问题
STM32 uses USB virtual serial port + ymodem to upgrade IAP
运维(33) CentOS7.6通过Kubeadm部署Kubernetes集群
D trigger in FPGA
Xshell7、Xftp7、Xlpd7-下载与安装教程(亲测可用)
动态数据库工具——Database Inspector
时间戳转换
[untitled] leetcode 396 rotation function [finding laws in mathematics] the way of leetcode in heroding
Matlab learning notes - calculate eigenvectors and manually execute PCA
Time and date formatting
Windows安装Redis
Microservice architecture from 0 to 1, build the whole process record, hand-in-hand teaching, serialization
06. Refactoring - simplifying conditional expressions
mysql经纬度 某半径长度 内查询数据
Solve the problem that the timeline does not match when the gold warehouse database kingbasees uses -- force rewind to forcibly pull up the standby machine
How to improve the efficiency of PHP programming?








