Skip to content

Commit 003bbd2

Browse files
committed
add 909 918 2938
1 parent 1392cb8 commit 003bbd2

28 files changed

+677
-175
lines changed

assets/output/0427.md

Lines changed: 59 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
🟠 <font color=#ffb800>Medium</font>&emsp; 🔖&ensp; [``](/leetcode/outline/tag/tree.md) [`数组`](/leetcode/outline/tag/array.md) [`分治`](/leetcode/outline/tag/divide-and-conquer.md) [`矩阵`](/leetcode/outline/tag/matrix.md)&emsp; 🔗&ensp;[`LeetCode`](https://leetcode.com/problems/construct-quad-tree)
44

5-
65
## 题目
76

87
Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent
@@ -13,42 +12,26 @@ Return _the root of the Quad-Tree representing_`grid`.
1312
A Quad-Tree is a tree data structure in which each internal node has exactly
1413
four children. Besides, each node has two attributes:
1514

16-
* `val`: True if the node represents a grid of 1's or False if the node represents a grid of 0's. Notice that you can assign the `val` to True or False when `isLeaf` is False, and both are accepted in the answer.
17-
* `isLeaf`: True if the node is a leaf node on the tree or False if the node has four children.
18-
19-
>
20-
>
21-
>
22-
>
23-
>
24-
> class Node {
25-
>
26-
> >
27-
> public boolean val;
28-
>
29-
> >
30-
> public boolean isLeaf;
31-
>
32-
> >
33-
> public Node topLeft;
34-
>
35-
> >
36-
> public Node topRight;
37-
>
38-
> >
39-
> public Node bottomLeft;
40-
>
41-
> >
42-
> public Node bottomRight;
43-
>
44-
> }
15+
- `val`: True if the node represents a grid of 1's or False if the node represents a grid of 0's. Notice that you can assign the `val` to True or False when `isLeaf` is False, and both are accepted in the answer.
16+
- `isLeaf`: True if the node is a leaf node on the tree or False if the node has four children.
17+
18+
```
19+
class Node {
20+
public boolean val;
21+
public boolean isLeaf;
22+
public Node topLeft;
23+
public Node topRight;
24+
public Node bottomLeft;
25+
public Node bottomRight;
26+
}
27+
```
4528

4629
We can construct a Quad-Tree from a two-dimensional area using the following
4730
steps:
4831

49-
1. If the current grid has the same value (i.e all `1's` or all `0's`) set `isLeaf` True and set `val` to the value of the grid and set the four children to Null and stop.
50-
2. If the current grid has different values, set `isLeaf` to False and set `val` to any value and divide the current grid into four sub-grids as shown in the photo.
51-
3. Recurse for each of the children with the proper sub-grid.
32+
1. If the current grid has the same value (i.e all `1's` or all `0's`) set `isLeaf` True and set `val` to the value of the grid and set the four children to Null and stop.
33+
2. If the current grid has different values, set `isLeaf` to False and set `val` to any value and divide the current grid into four sub-grids as shown in the photo.
34+
3. Recurse for each of the children with the proper sub-grid.
5235

5336
![](https://assets.leetcode.com/uploads/2020/02/11/new_top.png)
5437

@@ -69,89 +52,70 @@ If the value of `isLeaf` or `val` is True we represent it as **1** in the list
6952
`[isLeaf, val]` and if the value of `isLeaf` or `val` is False we represent it
7053
as **0**.
7154

72-
73-
7455
**Example 1:**
7556

7657
![](https://assets.leetcode.com/uploads/2020/02/11/grid1.png)
7758

7859
> Input: grid = [[0,1],[1,0]]
79-
>
60+
>
8061
> Output: [[0,1],[1,0],[1,1],[1,1],[1,0]]
81-
>
62+
>
8263
> Explanation: The explanation of this example is shown below:
83-
>
64+
>
8465
> Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree.
85-
>
66+
>
8667
> ![](https://assets.leetcode.com/uploads/2020/02/12/e1tree.png)
8768
8869
**Example 2:**
8970

9071
![](https://assets.leetcode.com/uploads/2020/02/12/e2mat.png)
9172

9273
> Input: grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]]
93-
>
74+
>
9475
> Output: [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
95-
>
76+
>
9677
> Explanation: All values in the grid are not the same. We divide the grid into four sub-grids.
97-
>
78+
>
9879
> The topLeft, bottomLeft and bottomRight each has the same value.
99-
>
80+
>
10081
> The topRight have different values so we divide it into 4 sub-grids where each has the same value.
101-
>
82+
>
10283
> Explanation is shown in the photo below:
103-
>
84+
>
10485
> ![](https://assets.leetcode.com/uploads/2020/02/12/e2tree.png)
10586
10687
**Constraints:**
10788

108-
* `n == grid.length == grid[i].length`
109-
* `n == 2x` where `0 <= x <= 6`
110-
89+
- `n == grid.length == grid[i].length`
90+
- `n == 2^x` where `0 <= x <= 6`
11191

11292
## 题目大意
11393

11494
给你一个 `n * n` 矩阵 `grid` ,矩阵由若干 `0``1` 组成。请你用四叉树表示该矩阵 `grid`
11595

116-
你需要返回能表示矩阵 `grid` 的 四叉树 的根结点。
96+
你需要返回能表示矩阵 `grid`**四叉树** 的根结点。
11797

11898
四叉树数据结构中,每个内部节点只有四个子节点。此外,每个节点都有两个属性:
11999

120-
* `val`:储存叶子结点所代表的区域的值。1 对应 **True** ,0 对应 **False** 。注意,当 `isLeaf`**False** 时,你可以把 **True** 或者 **False** 赋值给节点,两种值都会被判题机制 **接受**
121-
* `isLeaf`: 当这个节点是一个叶子结点时为 **True** ,如果它有 4 个子节点则为 **False**
122-
123-
>
124-
>
125-
>
126-
>
127-
>
128-
> class Node {
129-
>
130-
> >
131-
> public boolean val;
132-
>
133-
> >
134-
> public boolean isLeaf;
135-
>
136-
> >
137-
> public Node topLeft;
138-
>
139-
> >
140-
> public Node topRight;
141-
>
142-
> >
143-
> public Node bottomLeft;
144-
>
145-
> >
146-
> public Node bottomRight;
147-
>
148-
> }
100+
- `val`:储存叶子结点所代表的区域的值。`1` 对应 **True**`0` 对应 **False** 。注意,当 `isLeaf`**False** 时,你可以把 **True** 或者 **False** 赋值给节点,两种值都会被判题机制 **接受**
101+
- `isLeaf`: 当这个节点是一个叶子结点时为 **True** ,如果它有 `4` 个子节点则为 **False**
102+
103+
```
104+
class Node {
105+
public boolean val;
106+
public boolean isLeaf;
107+
public Node topLeft;
108+
public Node topRight;
109+
public Node bottomLeft;
110+
public Node bottomRight;
111+
}
112+
```
149113

150114
我们可以按以下步骤为二维区域构建四叉树:
151115

152-
1. 如果当前网格的值相同(即,全为 `0` 或者全为 `1`),将 `isLeaf` 设为 True ,将 `val` 设为网格相应的值,并将四个子节点都设为 Null 然后停止。
153-
2. 如果当前网格的值不同,将 `isLeaf` 设为 False, 将 `val` 设为任意值,然后如下图所示,将当前网格划分为四个子网格。
154-
3. 使用适当的子网格递归每个子节点。
116+
1. 如果当前网格的值相同(即,全为 `0` 或者全为 `1`),将 `isLeaf` 设为 True ,将 `val` 设为网格相应的值,并将四个子节点都设为 Null 然后停止。
117+
2. 如果当前网格的值不同,将 `isLeaf` 设为 False, 将 `val` 设为任意值,然后如下图所示,将当前网格划分为四个子网格。
118+
3. 使用适当的子网格递归每个子节点。
155119

156120
![](https://assets.leetcode.com/uploads/2020/02/11/new_top.png)
157121

@@ -165,63 +129,44 @@ as **0**.
165129
它与二叉树的序列化非常相似。唯一的区别是节点以列表形式表示 `[isLeaf, val]`
166130

167131
如果 `isLeaf` 或者 `val` 的值为 True ,则表示它在列表 `[isLeaf, val]` 中的值为 **1** ;如果 `isLeaf`
168-
或者 `val` 的值为 False ,则表示值为 **0**
169-
170-
132+
或者 `val` 的值为 `False` ,则表示值为 **0**
171133

172134
**示例 1:**
173135

174136
![](https://assets.leetcode.com/uploads/2020/02/11/grid1.png)
175137

176-
>
177-
>
178-
>
179-
>
180-
>
181138
> **输入:** grid = [[0,1],[1,0]]
182-
>
139+
>
183140
> **输出:**[[0,1],[1,0],[1,1],[1,1],[1,0]]
184-
>
141+
>
185142
> **解释:** 此示例的解释如下:
186-
>
143+
>
187144
> 请注意,在下面四叉树的图示中,0 表示 false,1 表示 True 。
188-
>
145+
>
189146
> ![](https://assets.leetcode.com/uploads/2020/02/12/e1tree.png)
190-
>
191-
>
192147
193148
**示例 2:**
194149

195150
![](https://assets.leetcode.com/uploads/2020/02/12/e2mat.png)
196151

197-
>
198-
>
199-
>
200-
>
201-
>
202152
> **输入:** grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]]
203-
>
153+
>
204154
> **输出:**[[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
205-
>
155+
>
206156
> **解释:** 网格中的所有值都不相同。我们将网格划分为四个子网格。
207-
>
157+
>
208158
> topLeft,bottomLeft 和 bottomRight 均具有相同的值。
209-
>
159+
>
210160
> topRight 具有不同的值,因此我们将其再分为 4 个子网格,这样每个子网格都具有相同的值。
211-
>
161+
>
212162
> 解释如下图所示:
213-
>
163+
>
214164
> ![](https://assets.leetcode.com/uploads/2020/02/12/e2tree.png)
215-
>
216-
>
217-
218-
219165
220166
**提示:**
221167

222-
1. `n == grid.length == grid[i].length`
223-
2. `n == 2x` 其中 `0 <= x <= 6`
224-
168+
1. `n == grid.length == grid[i].length`
169+
2. `n == 2^x` 其中 `0 <= x <= 6`
225170

226171
## 解题思路
227172

@@ -234,4 +179,4 @@ as **0**.
234179

235180
```javascript
236181

237-
```
182+
```

src/.vuepress/sidebar.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ export default sidebar({
406406
text: "0900-0999",
407407
collapsible: true,
408408
children: [
409+
"0909",
410+
"0918",
409411
"0921",
410412
"0931",
411413
"0945",
@@ -527,6 +529,13 @@ export default sidebar({
527529
"2696"
528530
],
529531
},
532+
{
533+
text: "2900-2999",
534+
collapsible: true,
535+
children: [
536+
"2938"
537+
],
538+
},
530539
{
531540
text: "3300-3399",
532541
collapsible: true,

src/leetcode/algorithm/dynamic_programming.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
| 354 | [俄罗斯套娃信封问题](https://leetcode.com/problems/russian-doll-envelopes) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/0354) | [`数组`](/leetcode/outline/tag/array.md) [`二分查找`](/leetcode/outline/tag/binary-search.md) [`动态规划`](/leetcode/outline/tag/dynamic-programming.md) `1+` | <font color=#ff334b>Hard</font> |
7474
| 53 | [最大子数组和](https://leetcode.com/problems/maximum-subarray) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/0053) | [`数组`](/leetcode/outline/tag/array.md) [`分治`](/leetcode/outline/tag/divide-and-conquer.md) [`动态规划`](/leetcode/outline/tag/dynamic-programming.md) | <font color=#ffb800>Medium</font> |
7575
| 152 | [乘积最大子数组](https://leetcode.com/problems/maximum-product-subarray) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/0152) | [`数组`](/leetcode/outline/tag/array.md) [`动态规划`](/leetcode/outline/tag/dynamic-programming.md) | <font color=#ffb800>Medium</font> |
76-
| 918 | [环形子数组的最大和](https://leetcode.com/problems/maximum-sum-circular-subarray) | | [`队列`](/leetcode/outline/tag/queue.md) [`数组`](/leetcode/outline/tag/array.md) [`分治`](/leetcode/outline/tag/divide-and-conquer.md) `2+` | <font color=#ffb800>Medium</font> |
76+
| 918 | [环形子数组的最大和](https://leetcode.com/problems/maximum-sum-circular-subarray) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/0918) | [`队列`](/leetcode/outline/tag/queue.md) [`数组`](/leetcode/outline/tag/array.md) [`分治`](/leetcode/outline/tag/divide-and-conquer.md) `2+` | <font color=#ffb800>Medium</font> |
7777
| 198 | [打家劫舍](https://leetcode.com/problems/house-robber) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/0198) | [`数组`](/leetcode/outline/tag/array.md) [`动态规划`](/leetcode/outline/tag/dynamic-programming.md) | <font color=#ffb800>Medium</font> |
7878
| 213 | [打家劫舍 II](https://leetcode.com/problems/house-robber-ii) | | [`数组`](/leetcode/outline/tag/array.md) [`动态规划`](/leetcode/outline/tag/dynamic-programming.md) | <font color=#ffb800>Medium</font> |
7979
| 740 | [删除并获得点数](https://leetcode.com/problems/delete-and-earn) | | [`数组`](/leetcode/outline/tag/array.md) [`哈希表`](/leetcode/outline/tag/hash-table.md) [`动态规划`](/leetcode/outline/tag/dynamic-programming.md) | <font color=#ffb800>Medium</font> |

src/leetcode/outline/solution_list.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 1.3 LeetCode 题解
22

3-
已完成 399
3+
已完成 402
44

55
<!-- prettier-ignore -->
66
| 题号 | 标题 | 题解 | 标签 | 难度 |
@@ -301,6 +301,8 @@
301301
| 876 | [链表的中间结点](https://leetcode.com/problems/middle-of-the-linked-list) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/0876) | [`链表`](/leetcode/outline/tag/linked-list.md) [`双指针`](/leetcode/outline/tag/two-pointers.md) | <font color=#15bd66>Easy</font> |
302302
| 880 | [索引处的解码字符串](https://leetcode.com/problems/decoded-string-at-index) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/0880) | [``](/leetcode/outline/tag/stack.md) [`字符串`](/leetcode/outline/tag/string.md) | <font color=#ffb800>Medium</font> |
303303
| 889 | [根据前序和后序遍历构造二叉树](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/0889) | [``](/leetcode/outline/tag/tree.md) [`数组`](/leetcode/outline/tag/array.md) [`哈希表`](/leetcode/outline/tag/hash-table.md) `2+` | <font color=#ffb800>Medium</font> |
304+
| 909 | [蛇梯棋](https://leetcode.com/problems/snakes-and-ladders) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/0909) | [`广度优先搜索`](/leetcode/outline/tag/breadth-first-search.md) [`数组`](/leetcode/outline/tag/array.md) [`矩阵`](/leetcode/outline/tag/matrix.md) | <font color=#ffb800>Medium</font> |
305+
| 918 | [环形子数组的最大和](https://leetcode.com/problems/maximum-sum-circular-subarray) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/0918) | [`队列`](/leetcode/outline/tag/queue.md) [`数组`](/leetcode/outline/tag/array.md) [`分治`](/leetcode/outline/tag/divide-and-conquer.md) `2+` | <font color=#ffb800>Medium</font> |
304306
| 921 | [使括号有效的最少添加](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/0921) | [``](/leetcode/outline/tag/stack.md) [`贪心`](/leetcode/outline/tag/greedy.md) [`字符串`](/leetcode/outline/tag/string.md) | <font color=#ffb800>Medium</font> |
305307
| 931 | [下降路径最小和](https://leetcode.com/problems/minimum-falling-path-sum) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/0931) | [`数组`](/leetcode/outline/tag/array.md) [`动态规划`](/leetcode/outline/tag/dynamic-programming.md) [`矩阵`](/leetcode/outline/tag/matrix.md) | <font color=#ffb800>Medium</font> |
306308
| 945 | [使数组唯一的最小增量](https://leetcode.com/problems/minimum-increment-to-make-array-unique) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/0945) | [`贪心`](/leetcode/outline/tag/greedy.md) [`数组`](/leetcode/outline/tag/array.md) [`计数`](/leetcode/outline/tag/counting.md) `1+` | <font color=#ffb800>Medium</font> |
@@ -342,6 +344,7 @@
342344
| 2491 | [划分技能点相等的团队](https://leetcode.com/problems/divide-players-into-teams-of-equal-skill) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/2491) | [`数组`](/leetcode/outline/tag/array.md) [`哈希表`](/leetcode/outline/tag/hash-table.md) [`双指针`](/leetcode/outline/tag/two-pointers.md) `1+` | <font color=#ffb800>Medium</font> |
343345
| 2530 | [执行 K 次操作后的最大分数](https://leetcode.com/problems/maximal-score-after-applying-k-operations) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/2530) | [`贪心`](/leetcode/outline/tag/greedy.md) [`数组`](/leetcode/outline/tag/array.md) [`堆(优先队列)`](/leetcode/outline/tag/heap-priority-queue.md) | <font color=#ffb800>Medium</font> |
344346
| 2696 | [删除子串后的字符串最小长度](https://leetcode.com/problems/minimum-string-length-after-removing-substrings) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/2696) | [``](/leetcode/outline/tag/stack.md) [`字符串`](/leetcode/outline/tag/string.md) [`模拟`](/leetcode/outline/tag/simulation.md) | <font color=#15bd66>Easy</font> |
347+
| 2938 | [区分黑球与白球](https://leetcode.com/problems/separate-black-and-white-balls) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/2938) | [`贪心`](/leetcode/outline/tag/greedy.md) [`双指针`](/leetcode/outline/tag/two-pointers.md) [`字符串`](/leetcode/outline/tag/string.md) | <font color=#ffb800>Medium</font> |
345348
| 3309 | [连接二进制表示可形成的最大数值](https://leetcode.com/problems/maximum-possible-number-by-binary-concatenation) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/3309) | [`位运算`](/leetcode/outline/tag/bit-manipulation.md) [`数组`](/leetcode/outline/tag/array.md) [`枚举`](/leetcode/outline/tag/enumeration.md) | <font color=#ffb800>Medium</font> |
346349
| 3318 | [计算子数组的 x-sum I](https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-i) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/3318) | | <font color=#15bd66>Easy</font> |
347350
| 3319 | [第 K 大的完美二叉子树的大小](https://leetcode.com/problems/k-th-largest-perfect-subtree-size-in-binary-tree) | [[]](https://2xiao.github.io/leetcode-js/leetcode/problem/3319) | | <font color=#ffb800>Medium</font> |

0 commit comments

Comments
 (0)