当前位置:网站首页>386. 字典序排数(中等)-迭代-全排列
386. 字典序排数(中等)-迭代-全排列
2022-04-23 17:32:00 【hequnwang10】
一、题目描述
给你一个整数 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]
二、解题
迭代
共有 n 个数需要被处理,假设当前处理到的数为 j,根据字典序规则,在满足条件的前提下,我们优先在 j 的后面添加 0(即 j * 10 < n 满足),否则我们考虑将上一位回退并进行加一操作。
class Solution {
public List<Integer> lexicalOrder(int n) {
//迭代
List<Integer> res = new ArrayList<>();
int number = 1;
for(int i = 0;i<n;i++){
res.add(number);
//首先尝试在number后面加0
if(number * 10 <= n){
number *= 10;
}else{
//如果加0后大于n了,则每次只加1.当然也需要判断是否进位 比如19,29,39。。。达到进位要求然后回退上一位。
while( number % 10 == 9 || number+1>n){
number /= 10;
}
number++;
}
}
return res;
}
}
时间复杂度:O(n);
空间复杂度:O(1)。
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://blog.csdn.net/hequnwang10/article/details/124355525
边栏推荐
- Further optimize Baidu map data visualization
- Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
- Simulation of infrared wireless communication based on 51 single chip microcomputer
- Net standard
- Solution architect's small bag - 5 types of architecture diagrams
- ASP. NET CORE3. 1. Solution to login failure after identity registers users
- El cascade and El select click elsewhere to make the drop-down box disappear
- Qt 修改UI没有生效
- node中,如何手动实现触发垃圾回收机制
- 1-5 nodejs commonjs specification
猜你喜欢

C# Task. Delay and thread The difference between sleep

Use of todesk remote control software

EF core in ASP Generate core priority database based on net entity model

快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
![[ES6] promise related (event loop, macro / micro task, promise, await / await)](/img/69/ea3ef6063d373f116a44c53565daa3.png)
[ES6] promise related (event loop, macro / micro task, promise, await / await)

练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)

2.Electron之HelloWorld

In embedded system, must the program code in flash be moved to ram to run?
![[registration] tf54: engineer growth map and excellent R & D organization building](/img/12/7aece45fbc9643c97cdda94b405118.jpg)
[registration] tf54: engineer growth map and excellent R & D organization building
![Using quartz under. Net core - [1] quick start](/img/80/b99417e88d544ca6e3da4c0c1625ce.png)
Using quartz under. Net core - [1] quick start
随机推荐
[二叉数] 二叉树的最大深度+N叉树的最大深度
node中,如何手动实现触发垃圾回收机制
1217_使用SCons生成目标文件
2.Electron之HelloWorld
C# Task. Delay and thread The difference between sleep
基于51单片机红外无线通讯仿真
Use of five routing guards
.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)
How to manually implement the mechanism of triggering garbage collection in node
1-2 JSX syntax rules
Some problems encountered in recent programming 2021 / 9 / 8
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
Entity Framework core captures database changes
[C] thoroughly understand the deep copy
1-5 nodejs commonjs specification
On lambda powertools typescript
[C#] 彻底搞明白深拷贝
双指针进阶--leetcode题目--盛最多水的容器
[markdown notes]
Low code development platform sorting