当前位置:网站首页>【dfs】386. 字典序排数
【dfs】386. 字典序排数
2022-04-22 20:19:00 【wjh776a68】
题目
给你一个整数 n ,按字典序返回范围 [1, n] 内所有整数。
你必须设计一个时间复杂度为 O(n) 且使用 O(1) 额外空间的算法。
示例 1:
输入:n = 13
输出:[1,10,11,12,13,2,3,4,5,6,7,8,9]
示例 2:
输入:n = 2
输出:[1,2]
提示:
- 1 <= n <= 5 * 104
答案
使用常量空间的dfs
链接
解
class Solution {
public:
vector<int> lexicalOrder(int n) {
//使用栈的dfs,能通过,但不确定是否满足题目要求的时空复杂度
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://blog.csdn.net/qq_45434284/article/details/124287298
边栏推荐
- Application of C language bit field -- storing eight flag bits in one byte
- 行业趋势远比努力更重要--顶测科技总结
- ZTNA (Zero Trust Network Access)
- 隔壁的wifi,我秒破
- 为了这一次字节跳动Android面试机会,我准备了158天,一个疏忽让我前功尽弃
- Boot implementation of IAP
- Lithography giant ASML broke the news: the chip is too short, and they are starting to dismantle the washing machine!
- 金仓数据库KingbaseES之null和“ ”的区别
- redis集群6.2.6
- 解决金仓数据库KingbaseES无法打开锁文件的问题
猜你喜欢

行业趋势远比努力更重要--顶测科技总结

SCI/SSCI期刊列表已更新,这几本期刊被剔除~

396. 旋转函数(数学规律 + 迭代法)

重保战场的“排头兵”,“互联网宙斯盾”如何为城市高效布防?

Why do I suggest you work in a technical position instead of a clerical position, sales

IAP之boot实现

ZTNA (Zero Trust Network Access)

What is the reason why the camera device with built-in 4G card of Haikang cannot register with easycvr platform?

396. Rotation function (mathematical law + iterative method)

Android面试题之屏幕适配+AIDL篇
随机推荐
Baidu has been listed in the delisting list of the United States, ICLR 2022 outstanding papers have been published, Tsinghua quantum computing startup has surfaced, and more big news is here today
解决金仓数据库KingbaseES使用--force-rewind强制拉起备机时,时间线不吻合的问题
Why do I suggest you work in a technical position instead of a clerical position, sales
故障分析 | Federated 存储引擎表导致监控线程处于 Opening table 状态
STM32使用USB虚拟串口+YMODEM实现IAP升级
(L2-026)小字辈(带权并查集)
年薪170W阿里P8相亲要求女方月薪1万,网友:有点高
Time and date formatting
[unity] pit records of Luna playable plug-ins that can play advertisements
Design of high performance and high availability architecture for microservices
Mysql, in the unique index of combination, handles the problem of null value
Application of C language bit field -- storing eight flag bits in one byte
为什么我建议你从事技术岗,而非文职,销售
Redis cluster 6.2.6
Don't say, "I don't like development, so I choose testing."
金仓数据库KingbaseES服务启动方法
如何提高PHP编程的效率?
时间戳转换
TikTok+Shopify独立站测评,卖家新风口.
Improving Few-Shot Part Segmentation using Coarse Supervision学习笔记