当前位置:网站首页>力扣解法汇总386-字典序排数
力扣解法汇总386-字典序排数
2022-06-12 02:04:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
给你一个整数 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
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/lexicographical-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 我们可以按照字典序的方式,使用递归的方式来解决该问题。 * 如果输入0,可以推导出0-9。然后其中的每个数都可以扩展出10个,比如1又可以推导出10-19。同理,10又可以推导出100到109,都是1扩10的方式进行的。 * 所以我们可以把输入的数,循环10遍每次加1,如果没超过则加入到队列中。并且对于每个数,都乘以10继续循环下去。
代码:
public class Solution386 {
public List<Integer> lexicalOrder(int n) {
ArrayList<Integer> result = new ArrayList<>();
add2List(result, 0, n);
return result;
}
/**
* @param result
* @param base 1,10,100
* @param maxValue
*/
private void add2List(ArrayList<Integer> result, int base, int maxValue) {
if (base > maxValue) {
return;
}
for (int i = 0; i < 10; i++) {
int current = base + i;
if (current > maxValue) {
break;
}
if (current == 0) {
continue;
}
result.add(current);
add2List(result, current * 10, maxValue);
}
}
}边栏推荐
- Smartbi helps you solve the problem of losing high-value customers
- Make ads more relevant by searching for additional information about ads
- 力扣解法汇总-04.06. 后继者
- redis集群(cluster)+哨兵模式+主从(replicas)
- 力扣解法汇总699-掉落的方块
- How to stop anti-virus software from blocking a web page? Take gdata as an example
- 通用树形结构的迭代与组合模式实现方案
- What are the preparations for setting up Google search advertising series?
- [adjustment] in 2022, the Key Laboratory of laser life sciences of the Ministry of education of South China Normal University enrolled adjustment students in optics, electronic information, biomedicin
- PHP security development 13 column module of blog system
猜你喜欢

Graphical data analysis | business analysis and data mining

Almost all schools will ask for the second round exam! Come in and recite the answer!

How to automatically color cells in Excel

Introduction to SVM

消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)

决定广告质量的三个主要因素

php开发 博客系统的公告模块的建立和引入

Unit tests in golang

The establishment and introduction of the announcement module of PHP development blog system

How WPS inserts a directory and the operating steps for quickly inserting a directory
随机推荐
微信公众号开发地理位置坐标的转换
通过搜索广告附加信息让广告更具相关性
lua 函数
力扣解法汇总699-掉落的方块
螺旋矩阵(技巧)
Ozzanmation action system based on SSE
Graphic data analysis | business cognition and data exploration
Fatal error in launcher: unable to create process using
In Net platform using reflectiondynamicobject to optimize reflection calling code
力扣解法汇总821-字符的最短距离
力扣解法汇总面试题 01.05. 一次编辑
力扣解法汇总-剑指 Offer II 114. 外星文字典
Don't miss it! Five large data visualization screens that HR must collect
leetcodeSQL:612. Nearest distance on plane
通用树形结构的迭代与组合模式实现方案
程序员应该如何解决买菜难问题?手把手带你利用无障碍辅助功能快速下单抢菜
如何提高广告的广告评级,也就是质量得分?
Modification of system module information of PHP security development 12 blog system
力扣解法汇总944-删列造序
“中国东信杯”广西大学第四届程序设计竞赛(同步赛)