Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b85fec1

Browse files
✨feat: Add 386
1 parent 62de943 commit b85fec1

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

‎Index/DFS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
| [301. 删除无效的括号](https://leetcode-cn.com/problems/remove-invalid-parentheses/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-invalid-parentheses/solution/yi-fen-zhong-nei-kan-dong-jiang-gua-hao-aya6k/) | 困难 | 🤩🤩🤩🤩 |
1111
| [310. 最小高度树](https://leetcode-cn.com/problems/minimum-height-trees/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-height-trees/solution/by-ac_oier-7xio/) | 中等 | 🤩🤩🤩🤩🤩 |
1212
| [341. 扁平化嵌套列表迭代器](https://leetcode-cn.com/problems/flatten-nested-list-iterator/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/flatten-nested-list-iterator/solution/yi-ti-shuang-jie-dfsdui-lie-di-gui-zhan-kvwhy/) | 中等 | 🤩🤩🤩 |
13+
| [386. 字典序排数](https://leetcode-cn.com/problems/lexicographical-numbers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lexicographical-numbers/solution/by-ac_oier-ktn7/) | 中等 | 🤩🤩🤩🤩 |
1314
| [397. 整数替换](https://leetcode-cn.com/problems/integer-replacement/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/integer-replacement/solution/gong-shui-san-xie-yi-ti-san-jie-dfsbfs-t-373h/) | 中等 | 🤩🤩🤩🤩 |
1415
| [403. 青蛙过河](https://leetcode-cn.com/problems/frog-jump/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/frog-jump/solution/gong-shui-san-xie-yi-ti-duo-jie-jiang-di-74fw/) | 困难 | 🤩🤩🤩🤩 |
1516
| [437. 路径总和 III](https://leetcode-cn.com/problems/path-sum-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-sum-iii/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-q-usa7/) | 中等 | 🤩🤩🤩🤩 |
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[386. 字典序排数](https://leetcode-cn.com/problems/lexicographical-numbers/solution/by-ac_oier-ktn7/)** ,难度为 **中等**
4+
5+
Tag : 「DFS」、「递归」、「迭代」
6+
7+
8+
9+
给你一个整数 `n` ,按字典序返回范围 $[1, n]$ 内所有整数。
10+
11+
你必须设计一个时间复杂度为 $O(n)$ 且使用 $O(1)$ 额外空间的算法。
12+
13+
示例 1:
14+
```
15+
输入:n = 13
16+
17+
输出:[1,10,11,12,13,2,3,4,5,6,7,8,9]
18+
```
19+
示例 2:
20+
```
21+
输入:n = 2
22+
23+
输出:[1,2]
24+
```
25+
26+
提示:
27+
* 1ドル <= n <= 5 * 10^4$
28+
29+
---
30+
31+
### 递归
32+
33+
首先容易想到使用「递归」来实现 `DFS`
34+
35+
将 $[1, n]$ 的数按照字典序添加到答案,本质上是对一颗节点数量为 $n,ドル形态类似字典树的多阶树进行遍历,根节点为 0ドル,ドル需要被跳过,因此我们可以从树的第二层开始搜索。
36+
37+
树中每个节点的值为其搜索路径所代表的数字,且每个节点有 $[0, 9]$ 共 10ドル$ 个子节点。
38+
39+
![image.png](https://pic.leetcode-cn.com/1650246458-ahFpqe-image.png)
40+
41+
代码:
42+
```Java
43+
class Solution {
44+
List<Integer> ans = new ArrayList<>();
45+
public List<Integer> lexicalOrder(int n) {
46+
for (int i = 1; i <= 9; i++) dfs(i, n);
47+
return ans;
48+
}
49+
void dfs(int cur, int limit) {
50+
if (cur > limit) return ;
51+
ans.add(cur);
52+
for (int i = 0; i <= 9; i++) dfs(cur * 10 + i, limit);
53+
}
54+
}
55+
```
56+
* 时间复杂度:本质上在搜索一棵节点数量为 $n$ 的多阶树(形态类似于字典树),复杂度为 $O(n)$
57+
* 空间复杂度:忽略递归带来的额外空间开销,复杂度为 $O(1)$
58+
59+
---
60+
61+
### 迭代
62+
63+
递归具有额外的空间开销,为了实现严格的 $O(1)$ 空间,我们需要使用「迭代」来实现 `DFS`
64+
65+
共有 $n$ 个数需要被处理,假设当前处理到的数为 $j,ドル根据字典序规则,在满足条件的前提下,我们优先在 $j$ 的后面添加 0ドル$(即 $j * 10 < n$ 满足),否则我们考虑将上一位回退并进行加一操作。
66+
67+
代码:
68+
```Java
69+
class Solution {
70+
public List<Integer> lexicalOrder(int n) {
71+
List<Integer> ans = new ArrayList<>();
72+
for (int i = 0, j = 1; i < n; i++) {
73+
ans.add(j);
74+
if (j * 10 <= n) {
75+
j *= 10;
76+
} else {
77+
while (j % 10 == 9 || j + 1 > n) j /= 10;
78+
j++;
79+
}
80+
}
81+
return ans;
82+
}
83+
}
84+
```
85+
* 时间复杂度:$O(n)$
86+
* 空间复杂度:$O(1)$
87+
88+
---
89+
90+
### 最后
91+
92+
这是我们「刷穿 LeetCode」系列文章的第 `No.386` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
93+
94+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
95+
96+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
97+
98+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
99+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /