Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
248 changes: 124 additions & 124 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"clang-format": "^1.8.0",
"husky": "^9.1.7",
"lint-staged": "^15.2.10",
"prettier": "^3.4.2",
"prettier": "^3.3.2",
"prettier-plugin-sql-cst": "^0.14.0"
},
"lint-staged": {
Expand Down
2 changes: 1 addition & 1 deletion solution/0000-0099/0092.Reverse Linked List II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tags:

<!-- description:start -->

给你单链表的头指针 <code>head</code> 和两个整数 <code>left</code> 和 <code>right</code> ,其中 <code>left <= right</code> 。请你反转从位置 <code>left</code> 到位置 <code>right</code> 的链表节点,返回 <strong>反转后的链表</strong> 。
给你单链表的头指针 <code>head</code> 和两个整数  <code>left</code> 和 <code>right</code> ,其中  <code>left <= right</code> 。请你反转从位置 <code>left</code> 到位置 <code>right</code> 的链表节点,返回 <strong>反转后的链表</strong> 。

<p> </p>

Expand Down
2 changes: 1 addition & 1 deletion solution/0100-0199/0177.Nth Highest Salary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tags:

<!-- problem:start -->

# [177. 第N高的薪水](https://leetcode.cn/problems/nth-highest-salary)
# [177. 第 N 高的薪水](https://leetcode.cn/problems/nth-highest-salary)

[English Version](/solution/0100-0199/0177.Nth%20Highest%20Salary/README_EN.md)

Expand Down
2 changes: 1 addition & 1 deletion solution/0100-0199/0187.Repeated DNA Sequences/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tags:

<!-- problem:start -->

# [187. 重复的DNA序列](https://leetcode.cn/problems/repeated-dna-sequences)
# [187. 重复的 DNA 序列](https://leetcode.cn/problems/repeated-dna-sequences)

[English Version](/solution/0100-0199/0187.Repeated%20DNA%20Sequences/README_EN.md)

Expand Down
2 changes: 1 addition & 1 deletion solution/0100-0199/0191.Number of 1 Bits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ tags:

<!-- problem:start -->

# [191. 位1的个数](https://leetcode.cn/problems/number-of-1-bits)
# [191. 位 1 的个数](https://leetcode.cn/problems/number-of-1-bits)

[English Version](/solution/0100-0199/0191.Number%20of%201%20Bits/README_EN.md)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tags:

<!-- problem:start -->

# [215. 数组中的第K个最大元素](https://leetcode.cn/problems/kth-largest-element-in-an-array)
# [215. 数组中的第 K 个最大元素](https://leetcode.cn/problems/kth-largest-element-in-an-array)

[English Version](/solution/0200-0299/0215.Kth%20Largest%20Element%20in%20an%20Array/README_EN.md)

Expand Down
2 changes: 1 addition & 1 deletion solution/0300-0399/0342.Power of Four/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tags:

<!-- problem:start -->

# [342. 4的幂](https://leetcode.cn/problems/power-of-four)
# [342. 4 的幂](https://leetcode.cn/problems/power-of-four)

[English Version](/solution/0300-0399/0342.Power%20of%20Four/README_EN.md)

Expand Down
2 changes: 1 addition & 1 deletion solution/0300-0399/0382.Linked List Random Node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ solution.getRandom(); // 返回 3
solution.getRandom(); // 返回 2
solution.getRandom(); // 返回 2
solution.getRandom(); // 返回 3
// getRandom() 方法应随机返回 1、2、3中的一个,每个元素被返回的概率相等。</pre>
// getRandom() 方法应随机返回 1、2、3 中的一个,每个元素被返回的概率相等。</pre>

<p>&nbsp;</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tags:

<!-- problem:start -->

# [440. 字典序的第K小数字](https://leetcode.cn/problems/k-th-smallest-in-lexicographical-order)
# [440. 字典序的第 K 小数字](https://leetcode.cn/problems/k-th-smallest-in-lexicographical-order)

[English Version](/solution/0400-0499/0440.K-th%20Smallest%20in%20Lexicographical%20Order/README_EN.md)

Expand Down Expand Up @@ -55,10 +55,10 @@ tags:

我们将 $[1, n]$ 看作一棵 **十叉字典树(Trie)**:

- 每个节点是一个前缀,根节点为空串;
- 节点的子节点是当前前缀拼接上 $0 \sim 9$;
- 例如前缀 $1$ 会有子节点 $10, 11, \ldots, 19$,而 $10$ 会有 $100, 101, \ldots, 109$;
- 这种结构天然符合字典序遍历。
- 每个节点是一个前缀,根节点为空串;
- 节点的子节点是当前前缀拼接上 $0 \sim 9$;
- 例如前缀 $1$ 会有子节点 $10, 11, \ldots, 19$,而 $10$ 会有 $100, 101, \ldots, 109$;
- 这种结构天然符合字典序遍历。

```
Expand All @@ -74,8 +74,8 @@ tags:

每次我们计算当前前缀下有多少个合法数字(即以 $\textit{curr}$ 为前缀、且不超过 $n$ 的整数个数),记作 $\textit{count}(\text{curr})$:

- 如果 $k \ge \text{count}(\text{curr})$:说明目标不在这棵子树中,跳过整棵子树,前缀右移:$\textit{curr} \leftarrow \text{curr} + 1$,并更新 $k \leftarrow k - \text{count}(\text{curr})$;
- 否则:说明目标在当前前缀的子树中,进入下一层:$\textit{curr} \leftarrow \text{curr} \times 10$,并消耗一个前缀:$k \leftarrow k - 1$。
- 如果 $k \ge \text{count}(\text{curr})$:说明目标不在这棵子树中,跳过整棵子树,前缀右移:$\textit{curr} \leftarrow \text{curr} + 1$,并更新 $k \leftarrow k - \text{count}(\text{curr})$;
- 否则:说明目标在当前前缀的子树中,进入下一层:$\textit{curr} \leftarrow \text{curr} \times 10$,并消耗一个前缀:$k \leftarrow k - 1$。

每一层我们将当前区间扩大 $10$ 倍,向下延伸到更长的前缀,直到超出 $n$。

Expand Down
2 changes: 1 addition & 1 deletion solution/0400-0499/0468.Validate IP Address/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tags:

<!-- problem:start -->

# [468. 验证IP地址](https://leetcode.cn/problems/validate-ip-address)
# [468. 验证 IP 地址](https://leetcode.cn/problems/validate-ip-address)

[English Version](/solution/0400-0499/0468.Validate%20IP%20Address/README_EN.md)

Expand Down
2 changes: 1 addition & 1 deletion solution/0400-0499/0487.Max Consecutive Ones II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tags:

<!-- problem:start -->

# [487. 最大连续1的个数 II 🔒](https://leetcode.cn/problems/max-consecutive-ones-ii)
# [487. 最大连续 1 的个数 II 🔒](https://leetcode.cn/problems/max-consecutive-ones-ii)

[English Version](/solution/0400-0499/0487.Max%20Consecutive%20Ones%20II/README_EN.md)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tags:

<!-- problem:start -->

# [562. 矩阵中最长的连续1线段 🔒](https://leetcode.cn/problems/longest-line-of-consecutive-one-in-matrix)
# [562. 矩阵中最长的连续 1 线段 🔒](https://leetcode.cn/problems/longest-line-of-consecutive-one-in-matrix)

[English Version](/solution/0500-0599/0562.Longest%20Line%20of%20Consecutive%20One%20in%20Matrix/README_EN.md)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tags:

<!-- problem:start -->

# [570. 至少有5名直接下属的经理](https://leetcode.cn/problems/managers-with-at-least-5-direct-reports)
# [570. 至少有 5 名直接下属的经理](https://leetcode.cn/problems/managers-with-at-least-5-direct-reports)

[English Version](/solution/0500-0599/0570.Managers%20with%20at%20Least%205%20Direct%20Reports/README_EN.md)

Expand Down
2 changes: 1 addition & 1 deletion solution/0500-0599/0585.Investments in 2016/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tags:

<!-- problem:start -->

# [585. 2016年的投资](https://leetcode.cn/problems/investments-in-2016)
# [585. 2016 年的投资](https://leetcode.cn/problems/investments-in-2016)

[English Version](/solution/0500-0599/0585.Investments%20in%202016/README_EN.md)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tags:

<!-- problem:start -->

# [600. 不含连续1的非负整数](https://leetcode.cn/problems/non-negative-integers-without-consecutive-ones)
# [600. 不含连续 1 的非负整数](https://leetcode.cn/problems/non-negative-integers-without-consecutive-ones)

[English Version](/solution/0600-0699/0600.Non-negative%20Integers%20without%20Consecutive%20Ones/README_EN.md)

Expand Down Expand Up @@ -79,29 +79,29 @@ $$

我们首先获取数字 $n$ 的二进制长度,记为 $m$。然后根据题目信息,我们设计函数 $\textit{dfs}(i, \textit{pre}, \textit{limit})$,其中:

- 数字 $i$ 表示当前搜索到的位置,我们从数字的最高位开始搜索,即二进制字符串的首字符;
- 数字 $\textit{pre}$ 表示上一个数字二进制位上的数字,对于本题,$\textit{pre}$ 的初始值为 $0$;
- 布尔值 $\textit{limit}$ 表示可填的数字的限制,如果无限制,那么可以选择 $[0,1]$,否则,只能选择 $[0, \textit{up}]$。
- 数字 $i$ 表示当前搜索到的位置,我们从数字的最高位开始搜索,即二进制字符串的首字符;
- 数字 $\textit{pre}$ 表示上一个数字二进制位上的数字,对于本题,$\textit{pre}$ 的初始值为 $0$;
- 布尔值 $\textit{limit}$ 表示可填的数字的限制,如果无限制,那么可以选择 $[0,1]$,否则,只能选择 $[0, \textit{up}]$。

函数的执行过程如下:

如果 $i$ 超过了数字 $n$ 的长度,即 $i \lt 0$,说明搜索结束,直接返回 $1$。否则,我们从 $0$ 到 $\textit{up}$ 枚举位置 $i$ 的数字 $j$,对于每一个 $j$:

- 如果 $\textit{pre}$ 和 $j$ 都为 $1$,说明有连续的 $1$,直接跳过;
- 否则,我们递归到下一层,更新 $\textit{pre}$ 为 $j$,并将 $\textit{limit}$ 更新为 $\textit{limit}$ 与 $j$ 是否等于 $\textit{up}$ 的逻辑与。
- 如果 $\textit{pre}$ 和 $j$ 都为 $1$,说明有连续的 $1$,直接跳过;
- 否则,我们递归到下一层,更新 $\textit{pre}$ 为 $j$,并将 $\textit{limit}$ 更新为 $\textit{limit}$ 与 $j$ 是否等于 $\textit{up}$ 的逻辑与。

最后,我们将所有递归到下一层的结果累加,即为答案。

时间复杂度 $O(\log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为题目给定的正整数。

相似题目:

- [233. 数字 1 的个数](https://github.com/doocs/leetcode/blob/main/solution/0200-0299/0233.Number%20of%20Digit%20One/README.md)
- [357. 统计各位数字都不同的数字个数](https://github.com/doocs/leetcode/blob/main/solution/0300-0399/0357.Count%20Numbers%20with%20Unique%20Digits/README.md)
- [788. 旋转数字](https://github.com/doocs/leetcode/blob/main/solution/0700-0799/0788.Rotated%20Digits/README.md)
- [902. 最大为 N 的数字组合](https://github.com/doocs/leetcode/blob/main/solution/0900-0999/0902.Numbers%20At%20Most%20N%20Given%20Digit%20Set/README.md)
- [1012. 至少有 1 位重复的数字](https://github.com/doocs/leetcode/blob/main/solution/1000-1099/1012.Numbers%20With%20Repeated%20Digits/README.md)
- [2376. 统计特殊整数](https://github.com/doocs/leetcode/blob/main/solution/2300-2399/2376.Count%20Special%20Integers/README.md)
- [233. 数字 1 的个数](https://github.com/doocs/leetcode/blob/main/solution/0200-0299/0233.Number%20of%20Digit%20One/README.md)
- [357. 统计各位数字都不同的数字个数](https://github.com/doocs/leetcode/blob/main/solution/0300-0399/0357.Count%20Numbers%20with%20Unique%20Digits/README.md)
- [788. 旋转数字](https://github.com/doocs/leetcode/blob/main/solution/0700-0799/0788.Rotated%20Digits/README.md)
- [902. 最大为 N 的数字组合](https://github.com/doocs/leetcode/blob/main/solution/0900-0999/0902.Numbers%20At%20Most%20N%20Given%20Digit%20Set/README.md)
- [1012. 至少有 1 位重复的数字](https://github.com/doocs/leetcode/blob/main/solution/1000-1099/1012.Numbers%20With%20Repeated%20Digits/README.md)
- [2376. 统计特殊整数](https://github.com/doocs/leetcode/blob/main/solution/2300-2399/2376.Count%20Special%20Integers/README.md)

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ tags:

<!-- problem:start -->

# [668. 乘法表中第k小的数](https://leetcode.cn/problems/kth-smallest-number-in-multiplication-table)
# [668. 乘法表中第 k 小的数](https://leetcode.cn/problems/kth-smallest-number-in-multiplication-table)

[English Version](/solution/0600-0699/0668.Kth%20Smallest%20Number%20in%20Multiplication%20Table/README_EN.md)

Expand Down
2 changes: 1 addition & 1 deletion solution/0600-0699/0692.Top K Frequent Words/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tags:

<!-- problem:start -->

# [692. 前K个高频单词](https://leetcode.cn/problems/top-k-frequent-words)
# [692. 前 K 个高频单词](https://leetcode.cn/problems/top-k-frequent-words)

[English Version](/solution/0600-0699/0692.Top%20K%20Frequent%20Words/README_EN.md)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tags:

<!-- problem:start -->

# [698. 划分为k个相等的子集](https://leetcode.cn/problems/partition-to-k-equal-sum-subsets)
# [698. 划分为 k 个相等的子集](https://leetcode.cn/problems/partition-to-k-equal-sum-subsets)

[English Version](/solution/0600-0699/0698.Partition%20to%20K%20Equal%20Sum%20Subsets/README_EN.md)

Expand Down Expand Up @@ -252,14 +252,14 @@ function canPartitionKSubsets(nums: number[], k: number): boolean {

我们的目标是从全部元素中凑出 $k$ 个和为 $s$ 的子集。记当前子集的和为 $t$。在未划分第 $i$ 个元素时:

- 若 $t + \textit{nums}[i] \gt s$,说明第 $i$ 个元素不能被添加到当前子集中,由于我们对 $\textit{nums}$ 数组进行升序排列,因此数组 $\textit{nums}$ 从位置 $i$ 开始的所有元素都不能被添加到当前子集,直接返回 $\textit{false}$。
- 否则,将第 $i$ 个元素添加到当前子集中,状态变为 $\textit{state} | 2^i$,然后继续对未划分的元素进行搜索。需要注意的是,若 $t + \textit{nums}[i] = s$,说明恰好可以得到一个和为 $s$ 的子集,下一步将 $t$ 归零(可以通过 $(t + \textit{nums}[i]) \bmod s$ 实现),并继续划分下一个子集。
- 若 $t + \textit{nums}[i] \gt s$,说明第 $i$ 个元素不能被添加到当前子集中,由于我们对 $\textit{nums}$ 数组进行升序排列,因此数组 $\textit{nums}$ 从位置 $i$ 开始的所有元素都不能被添加到当前子集,直接返回 $\textit{false}$。
- 否则,将第 $i$ 个元素添加到当前子集中,状态变为 $\textit{state} | 2^i$,然后继续对未划分的元素进行搜索。需要注意的是,若 $t + \textit{nums}[i] = s$,说明恰好可以得到一个和为 $s$ 的子集,下一步将 $t$ 归零(可以通过 $(t + \textit{nums}[i]) \bmod s$ 实现),并继续划分下一个子集。

为了避免重复搜索,我们使用一个长度为 $2^n$ 的数组 $\textit{f}$ 记录每个状态下的搜索结果。数组 $\textit{f}$ 有三个可能的值:

- `0`:表示当前状态还未搜索过;
- `-1`:表示当前状态下无法划分为 $k$ 个子集;
- `1`:表示当前状态下可以划分为 $k$ 个子集。
- `0`:表示当前状态还未搜索过;
- `-1`:表示当前状态下无法划分为 $k$ 个子集;
- `1`:表示当前状态下可以划分为 $k$ 个子集。

时间复杂度 $O(n \times 2^n)$,空间复杂度 $O(2^n)$。其中 $n$ 表示数组 $\textit{nums}$ 的长度。对于每个状态,我们需要遍历数组 $\textit{nums}$,时间复杂度为 $O(n)$;状态总数为 $2^n$,因此总的时间复杂度为 $O(n\times 2^n)$。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ tags:

<!-- problem:start -->

# [712. 两个字符串的最小ASCII删除和](https://leetcode.cn/problems/minimum-ascii-delete-sum-for-two-strings)
# [712. 两个字符串的最小 ASCII 删除和](https://leetcode.cn/problems/minimum-ascii-delete-sum-for-two-strings)

[English Version](/solution/0700-0799/0712.Minimum%20ASCII%20Delete%20Sum%20for%20Two%20Strings/README_EN.md)

Expand Down Expand Up @@ -79,7 +79,7 @@ $$

相似题目:

- [1143. 最长公共子序列](https://github.com/doocs/leetcode/blob/main/solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md)
- [1143. 最长公共子序列](https://github.com/doocs/leetcode/blob/main/solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md)

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tags:

<!-- problem:start -->

# [757. 设置交集大小至少为2](https://leetcode.cn/problems/set-intersection-size-at-least-two)
# [757. 设置交集大小至少为 2](https://leetcode.cn/problems/set-intersection-size-at-least-two)

[English Version](/solution/0700-0799/0757.Set%20Intersection%20Size%20At%20Least%20Two/README_EN.md)

Expand Down
2 changes: 1 addition & 1 deletion solution/0700-0799/0779.K-th Symbol in Grammar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tags:

<!-- problem:start -->

# [779. 第K个语法符号](https://leetcode.cn/problems/k-th-symbol-in-grammar)
# [779. 第 K 个语法符号](https://leetcode.cn/problems/k-th-symbol-in-grammar)

[English Version](/solution/0700-0799/0779.K-th%20Symbol%20in%20Grammar/README_EN.md)

Expand Down
2 changes: 1 addition & 1 deletion solution/0700-0799/0785.Is Graph Bipartite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tags:

<!-- description:start -->

存在一个 <strong>无向图</strong> ,图中有 <code>n</code> 个节点。其中每个节点都有一个介于 <code>0</code> 到 <code>n - 1</code> 之间的唯一编号。给你一个二维数组 <code>graph</code> ,其中 <code>graph[u]</code> 是一个节点数组,由节点 <code>u</code> 的邻接节点组成。形式上,对于 <code>graph[u]</code> 中的每个 <code>v</code> ,都存在一条位于节点 <code>u</code> 和节点 <code>v</code> 之间的无向边。该无向图同时具有以下属性:
存在一个 <strong>无向图</strong> ,图中有 <code>n</code> 个节点。其中每个节点都有一个介于 <code>0</code> 到 <code>n - 1</code> 之间的唯一编号。给你一个二维数组 <code>graph</code> ,其中 <code>graph[u]</code> 是一个节点数组,由节点 <code>u</code> 的邻接节点组成。形式上,对于  <code>graph[u]</code> 中的每个 <code>v</code> ,都存在一条位于节点 <code>u</code> 和节点 <code>v</code> 之间的无向边。该无向图同时具有以下属性:

<ul>
<li>不存在自环(<code>graph[u]</code> 不包含 <code>u</code>)。</li>
Expand Down
16 changes: 8 additions & 8 deletions solution/0800-0899/0838.Push Dominoes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ tags:

<!-- solution:start -->

### 方法一:多源 BFS
### 方法一:多源  BFS

把所有初始受到推力的骨牌(`L`  `R`)视作 **源点**,它们会同时向外扩散各自的力。用队列按时间层级(0, 1, 2 …)进行 BFS:
把所有初始受到推力的骨牌(`L`  `R`)视作 **源点**,它们会同时向外扩散各自的力。用队列按时间层级(0, 1, 2 …)进行  BFS:

我们定义 $\text{time[i]}$ 记录第 *i* 张骨牌第一次受力的时刻,`-1` 表示尚未受力,定义 $\text{force[i]}$ 是一个长度可变的列表,存放该骨牌在同一时刻收到的方向(`'L'`、`'R'`)。初始时把所有 `L/R` 的下标压入队列,并将它们的时间置 0。
我们定义 $\text{time[i]}$  记录第  *i*  张骨牌第一次受力的时刻,`-1`  表示尚未受力,定义 $\text{force[i]}$  是一个长度可变的列表,存放该骨牌在同一时刻收到的方向(`'L'`、`'R'`)。初始时把所有  `L/R`  的下标压入队列,并将它们的时间置  0。

当弹出下标 *i* 时,若 $\text{force[i]}$ 只有一个方向,骨牌就会倒向该方向 $f$。设下一张骨牌下标为
当弹出下标  *i* 时,若  $\text{force[i]}$  只有一个方向,骨牌就会倒向该方向  $f$。设下一张骨牌下标为

$$
j =
Expand All @@ -84,12 +84,12 @@ i + 1, & f = R.
\end{cases}
$$

若 $0 \leq j < n$:
 $0 \leq j < n$:

- 若 $\text{time[j]}=-1$,说明 *j* 从未受力,记录 $\text{time[j]}=\text{time[i]}+1$ 并入队,同时把 $f$ 写入 $\text{force[j]}$。
- 若 $\text{time[j]}=\text{time[i]}+1$,说明它在同一“下一刻”已受过另一股力,此时只把 $f$ 追加到 $\text{force[j]}$,形成对冲;后续因 `len(force[j])==2`,它将保持竖直。
- 若 $\text{time[j]}=-1$,说明  *j*  从未受力,记录 $\text{time[j]}=\text{time[i]}+1$ 并入队,同时把  $f$ 写入  $\text{force[j]}$。
- 若 $\text{time[j]}=\text{time[i]}+1$,说明它在同一“下一刻”已受过另一股力,此时只把  $f$ 追加到  $\text{force[j]}$,形成对冲;后续因  `len(force[j])==2`,它将保持竖直。

队列清空后,所有 $\text{force[i]}$ 长度为 1 的位置倒向对应方向;长度为 2 的位置保持 `.`。最终将字符数组拼接为答案。
队列清空后,所有  $\text{force[i]}$  长度为  1  的位置倒向对应方向;长度为  2  的位置保持  `.`。最终将字符数组拼接为答案。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是骨牌的数量。

Expand Down
Loading
Loading