当前位置:网站首页>386. Dictionary order (medium) - iteration - full arrangement
386. Dictionary order (medium) - iteration - full arrangement
2022-04-23 17:33:00 【hequnwang10】
One 、 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]
Two 、 Problem solving
iteration
share n The number needs to be processed , Suppose the number of currently processed data is j, According to the dictionary order rule , On the premise that the conditions are met , Our priority is j Add 0( namely j * 10 < n Satisfy ), Otherwise, we consider fallback the previous bit and add one .
class Solution {
public List<Integer> lexicalOrder(int n) {
// iteration
List<Integer> res = new ArrayList<>();
int number = 1;
for(int i = 0;i<n;i++){
res.add(number);
// First try at number Back plus 0
if(number * 10 <= n){
number *= 10;
}else{
// If added 0 Later is greater than n 了 , Then add only one at a time 1. Of course, you also need to judge whether to carry such as 19,29,39... Reach the carry requirement and then go back to the previous bit .
while( number % 10 == 9 || number+1>n){
number /= 10;
}
number++;
}
}
return res;
}
}
Time complexity :O(n);
Spatial complexity :O(1).
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231732009048.html
边栏推荐
- 440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题
- 41. 缺失的第一个正数
- ASP. Net core JWT certification
- Using quartz under. Net core -- a simple trigger of [7] operation and trigger
- SiteServer CMS5. 0 Usage Summary
- [related to zhengheyuan cutting tools]
- Baidu Map Case - Zoom component, map scale component
- 1217_使用SCons生成目标文件
- How to sort the numbers with text in Excel from small to large instead of the first number
- Tdan over half
猜你喜欢

. net cross platform principle (Part I)

JVM类加载机制

.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)

stm32入门开发板选野火还是正点原子呢?

394. 字符串解码-辅助栈

双闭环直流调速系统matlab/simulink仿真
![[difference between Oracle and MySQL]](/img/90/6d030a35692fa27f1a7c63985af06f.png)
[difference between Oracle and MySQL]

JVM class loading mechanism

How to change input into text

Collection of common SQL statements
随机推荐
开期货,开户云安全还是相信期货公司的软件?
Header built-in object
C listens for WMI events
Generating access keys using JSON webtoken
MySQL installation
Use of Shell sort command
练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
matlab如何绘制已知公式的曲线图,Excel怎么绘制函数曲线图像?
48. 旋转图像
41. The first missing positive number
If you start from zero according to the frame
Router object, route object, declarative navigation, programmed navigation
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
Deep understanding of control inversion and dependency injection
为什么有些人说单片机简单,我学起来这么吃力?
古代埃及希腊,数学用的什么进制
XTask与Kotlin Coroutine的使用对比
ASP. Net core reads the configuration file in the class library project
给 el-dialog 增加拖拽功能