Skip to content

Commit 3c4a791

Browse files
LetMeFly666Copilot
andauthored
update: 添加问题“386.字典序排数”的代码和题解(#975)
* MGJW(words): archive (#974) Signed-off-by: LetMeFly666 <814114971@qq.com> * 386: WA.cpp (#974) Signed-off-by: LetMeFly666 <814114971@qq.com> * 386: AC.cpp+WA.py (#974) cpp - AC,100.00%,87.60% Signed-off-by: LetMeFly666 <814114971@qq.com> * 386: AC.py (#974) while now % 10 == 9和if now == n不可以分开 否则now==n后now/=10后now%10还是可能等于9 py - AC,97.55%,98.68% Signed-off-by: LetMeFly666 <814114971@qq.com> * 386+MGJW: CE.java (#974) Signed-off-by: LetMeFly666 <814114971@qq.com> * 386: CE.java (#974) Signed-off-by: LetMeFly666 <814114971@qq.com> * 386: AC.java+go (#974) java - AC,83.25%,83.25% go - AC,100.00%,89.92% Signed-off-by: LetMeFly666 <814114971@qq.com> * update: 添加问题“386.字典序排数”的代码和题解(#975) Signed-off-by: LetMeFly666 <814114971@qq.com> * fix: Update Solutions/LeetCode 0386.字典序排数.md (#864) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix: Update Solutions/LeetCode 0386.字典序排数.md (#864) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Signed-off-by: LetMeFly666 <814114971@qq.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 78e8df4 commit 3c4a791

29 files changed

+934
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-06-09 10:09:21
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-06-09 10:15:25
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
vector<int> lexicalOrder(int n) {
14+
vector<int> ans(n);
15+
int now = 1;
16+
for (int i = 0; i < n; i++) {
17+
ans[i] = now;
18+
if (now * 10 <= n) {
19+
now *= 10;
20+
} else {
21+
while (now % 10 == 9 || now == n) {
22+
now /= 10;
23+
}
24+
now++;
25+
}
26+
}
27+
return ans;
28+
}
29+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-06-09 10:09:21
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-06-09 22:03:40
6+
*/
7+
package main
8+
9+
func lexicalOrder(n int) []int {
10+
ans := make([]int, n)
11+
for now, i := 1, 0; i < n; i++ {
12+
ans[i] = now
13+
if now * 10 <= n {
14+
now *= 10
15+
} else {
16+
for now % 10 == 9 || now == n {
17+
now /= 10
18+
}
19+
now++
20+
}
21+
}
22+
return ans
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-06-09 10:09:21
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-06-09 22:00:48
6+
*/
7+
import java.util.List;
8+
import java.util.ArrayList;
9+
10+
class Solution {
11+
public List<Integer> lexicalOrder(int n) {
12+
List<Integer> ans = new ArrayList<>();
13+
for (int now = 1, i = 0; i < n; i++) {
14+
ans.add(now);
15+
if (now * 10 <= n) {
16+
now *= 10;
17+
} else {
18+
while (now % 10 == 9 || now == n) {
19+
now /= 10;
20+
}
21+
now++;
22+
}
23+
}
24+
return ans;
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-06-09 10:09:21
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-06-09 10:21:53
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def lexicalOrder(self, n: int) -> List[int]:
11+
ans = [0] * n
12+
now = 1
13+
for i in range(n):
14+
ans[i] = now
15+
if now * 10 <= n:
16+
now *= 10
17+
else:
18+
while now % 10 == 9 or now == n:
19+
now //= 10
20+
now += 1
21+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@
288288
|0377.组合总和Ⅳ|中等|<a href="https://leetcode.cn/problems/combination-sum-iv/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/10/LeetCode%200377.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C%E2%85%A3/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127251669" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/combination-sum-iv/solution/letmefly-377zu-he-zong-he-iv-by-tisfy-zp8z/" target="_blank">LeetCode题解</a>|
289289
|0381.O(1)时间插入、删除和获取随机元素-允许重复|困难|<a href="https://leetcode.cn/problems/insert-delete-getrandom-o1-duplicates-allowed/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/11/LeetCode%200381.O%281%29%E6%97%B6%E9%97%B4%E6%8F%92%E5%85%A5%E3%80%81%E5%88%A0%E9%99%A4%E5%92%8C%E8%8E%B7%E5%8F%96%E9%9A%8F%E6%9C%BA%E5%85%83%E7%B4%A0-%E5%85%81%E8%AE%B8%E9%87%8D%E5%A4%8D/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127262069" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/insert-delete-getrandom-o1-duplicates-allowed/solution/letmefly-stlde-ying-yong-381o1-shi-jian-6ngis/" target="_blank">LeetCode题解</a>|
290290
|0383.赎金信|简单|<a href="https://leetcode.cn/problems/ransom-note/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/07/LeetCode%200383.%E8%B5%8E%E9%87%91%E4%BF%A1/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135438384" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/ransom-note/solutions/2594460/letmefly-383shu-jin-xin-ji-shu-by-tisfy-lfty/" target="_blank">LeetCode题解</a>|
291+
|0386.字典序排数|中等|<a href="https://leetcode.cn/problems/lexicographical-numbers/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/06/09/LeetCode%200386.%E5%AD%97%E5%85%B8%E5%BA%8F%E6%8E%92%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148545551" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/lexicographical-numbers/solutions/3696853/letmefly-386zi-dian-xu-pai-shu-xi-xin-zo-48zm/" target="_blank">LeetCode题解</a>|
291292
|0387.字符串中的第一个唯一字符|简单|<a href="https://leetcode.cn/problems/first-unique-character-in-a-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/11/LeetCode%200387.%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%94%AF%E4%B8%80%E5%AD%97%E7%AC%A6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127262237" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/first-unique-character-in-a-string/solution/letmefly-387zi-fu-chuan-zhong-de-di-yi-g-gk9b/" target="_blank">LeetCode题解</a>|
292293
|0392.判断子序列|简单|<a href="https://leetcode.cn/problems/is-subsequence/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/12/LeetCode%200392.%E5%88%A4%E6%96%AD%E5%AD%90%E5%BA%8F%E5%88%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127279017" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/is-subsequence/solution/letmefly-392pan-duan-zi-xu-lie-by-tisfy-o9f1/" target="_blank">LeetCode题解</a>|
293294
|0395.至少有K个重复字符的最长子串|中等|<a href="https://leetcode.cn/problems/longest-substring-with-at-least-k-repeating-characters/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/13/LeetCode%200395.%E8%87%B3%E5%B0%91%E6%9C%89K%E4%B8%AA%E9%87%8D%E5%A4%8D%E5%AD%97%E7%AC%A6%E7%9A%84%E6%9C%80%E9%95%BF%E5%AD%90%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127296624" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-substring-with-at-least-k-repeating-characters/solution/by-tisfy-fv9c/" target="_blank">LeetCode题解</a>|
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
---
2+
title: 386.字典序排数:细心总结条件
3+
date: 2025-06-09 23:36:33
4+
tags: [题解, LeetCode, 中等, 深度优先搜索, 字典树]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】386.字典序排数:细心总结条件
9+
10+
力扣题目链接:[https://leetcode.cn/problems/lexicographical-numbers/](https://leetcode.cn/problems/lexicographical-numbers/)
11+
12+
<p>给你一个整数 <code>n</code> ,按字典序返回范围 <code>[1, n]</code> 内所有整数。</p>
13+
14+
<p>你必须设计一个时间复杂度为 <code>O(n)</code> 且使用 <code>O(1)</code> 额外空间的算法。</p>
15+
16+
<p>&nbsp;</p>
17+
18+
<p><strong>示例 1:</strong></p>
19+
20+
<pre>
21+
<strong>输入:</strong>n = 13
22+
<strong>输出:</strong>[1,10,11,12,13,2,3,4,5,6,7,8,9]
23+
</pre>
24+
25+
<p><strong>示例 2:</strong></p>
26+
27+
<pre>
28+
<strong>输入:</strong>n = 2
29+
<strong>输出:</strong>[1,2]
30+
</pre>
31+
32+
<p>&nbsp;</p>
33+
34+
<p><strong>提示:</strong></p>
35+
36+
<ul>
37+
<li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
38+
</ul>
39+
40+
41+
42+
## 解题方法:if-else
43+
44+
### 解题思路
45+
46+
使用O(1)的空间和O(n)的时间完成本题,不如先想想字典序下到底是个什么顺序。
47+
48+
1. 首先是1、10、100、...,直到即将大于n为止(假设n=200,那么100即将大于n(100*10=1000))
49+
2. 100后接着是101、102、...109
50+
3. 109之后是11(而不是110),之后进入第一步(11、110、...)
51+
52+
基本上就这三种情况。
53+
54+
### 具体做法
55+
56+
使用一个变量now记录当前的值,如果$now\times 10\leq n$就让$now\times 10$,否则:
57+
58+
> 就让$now+1$。特别的,在$now+1$之前,若$now$的最后一位已经是$9$或者$now$已经等于了$n$,就不断移除$now$的最后一位(例如,109变成10)。
59+
60+
### 时空复杂度分析
61+
62+
+ 时间复杂度$O(n)$
63+
+ 空间复杂度$O(1)$
64+
65+
### AC代码
66+
67+
#### C++
68+
69+
```cpp
70+
/*
71+
* @Author: LetMeFly
72+
* @Date: 2025-06-09 10:09:21
73+
* @LastEditors: LetMeFly.xyz
74+
* @LastEditTime: 2025-06-09 10:15:25
75+
*/
76+
class Solution {
77+
public:
78+
vector<int> lexicalOrder(int n) {
79+
vector<int> ans(n);
80+
int now = 1;
81+
for (int i = 0; i < n; i++) {
82+
ans[i] = now;
83+
if (now * 10 <= n) {
84+
now *= 10;
85+
} else {
86+
while (now % 10 == 9 || now == n) {
87+
now /= 10;
88+
}
89+
now++;
90+
}
91+
}
92+
return ans;
93+
}
94+
};
95+
```
96+
97+
#### Python
98+
99+
```python
100+
'''
101+
Author: LetMeFly
102+
Date: 2025-06-09 10:09:21
103+
LastEditors: LetMeFly.xyz
104+
LastEditTime: 2025-06-09 10:21:53
105+
'''
106+
from typing import List
107+
108+
class Solution:
109+
def lexicalOrder(self, n: int) -> List[int]:
110+
ans = [0] * n
111+
now = 1
112+
for i in range(n):
113+
ans[i] = now
114+
if now * 10 <= n:
115+
now *= 10
116+
else:
117+
while now % 10 == 9 or now == n:
118+
now //= 10
119+
now += 1
120+
return ans
121+
```
122+
123+
#### Java
124+
125+
```java
126+
/*
127+
* @Author: LetMeFly
128+
* @Date: 2025-06-09 10:09:21
129+
* @LastEditors: LetMeFly.xyz
130+
* @LastEditTime: 2025-06-09 22:00:48
131+
*/
132+
import java.util.List;
133+
import java.util.ArrayList;
134+
135+
class Solution {
136+
public List<Integer> lexicalOrder(int n) {
137+
List<Integer> ans = new ArrayList<>();
138+
for (int now = 1, i = 0; i < n; i++) {
139+
ans.add(now);
140+
if (now * 10 <= n) {
141+
now *= 10;
142+
} else {
143+
while (now % 10 == 9 || now == n) {
144+
now /= 10;
145+
}
146+
now++;
147+
}
148+
}
149+
return ans;
150+
}
151+
}
152+
```
153+
154+
#### Go
155+
156+
```go
157+
/*
158+
* @Author: LetMeFly
159+
* @Date: 2025-06-09 10:09:21
160+
* @LastEditors: LetMeFly.xyz
161+
* @LastEditTime: 2025-06-09 22:03:40
162+
*/
163+
package main
164+
165+
func lexicalOrder(n int) []int {
166+
ans := make([]int, n)
167+
for now, i := 1, 0; i < n; i++ {
168+
ans[i] = now
169+
if now * 10 <= n {
170+
now *= 10
171+
} else {
172+
for now % 10 == 9 || now == n {
173+
now /= 10
174+
}
175+
now++
176+
}
177+
}
178+
return ans
179+
}
180+
```
181+
182+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/148545551)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/06/09/LeetCode%200386.%E5%AD%97%E5%85%B8%E5%BA%8F%E6%8E%92%E6%95%B0/)哦~
183+
>
184+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,10 @@ categories: [自用]
12051205
|||
12061206
|stupidity|n. 愚蠢,愚蠢行为,糊涂,笨|
12071207
|mutton|n. 羊肉|
1208+
|||
1209+
|plume|n. 羽毛,飘升之物,一缕,(常用作装饰的)连在一起的羽毛<br/>v. 用羽毛装饰,(鸟)整理(羽毛),借衣服修饰,自夸|
1210+
|||
1211+
|indefinitely|adv. 无期限的|
12081212

12091213
<p class="wordCounts">单词收录总数</p>
12101214

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)