Skip to content

Commit faffb83

Browse files
committed
二刷221
1 parent b0b6866 commit faffb83

File tree

9 files changed

+124
-32
lines changed

9 files changed

+124
-32
lines changed

docs/0221-maximal-square.adoc

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,79 @@
11
[#0221-maximal-square]
2-
= 221. Maximal Square
2+
= 221. 最大正方形
33

4-
{leetcode}/problems/maximal-square/[LeetCode - Maximal Square^]
4+
https://leetcode.cn/problems/maximal-square/[LeetCode - 221. 最大正方形 ^]
55

6-
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
6+
在一个由 `0``1` 组成的二维矩阵内,找到只包含 `1` 的最大正方形,并返回其面积。
77

8-
.Example:
9-
----
10-
Input:
8+
*示例 1:*
119

12-
1 0 1 0 0
13-
1 0 1 1 1
14-
1 1 1 1 1
15-
1 0 0 1 0
10+
image::images/0221-01.jpg[{image_attr}]
1611

17-
Output: 4
18-
----
12+
....
13+
输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
14+
输出:4
15+
....
1916

20-
根据左上、左边、上边加自身,确定一个最小正方形;然后再进一步,在小正方形基础上,从四周选择已有正方形中最小,然后扩大,再这个过程中记录下最大的正方形边长,即可得到结果。思路如下图:
17+
*示例 2:*
2118

22-
image::images/0221-1.png[{image_attr}]
19+
image::images/0221-02.jpg[{image_attr}]
2320

24-
简化一下,不需要矩阵来存储所有正方形的计算结果,只需要一行来记录上一行计算结果和当前行计算结果即可。
21+
....
22+
输入:matrix = [["0","1"],["1","0"]]
23+
输出:1
24+
....
2525

26-
image::images/0221-2.png[{image_attr}]
26+
*示例 3:*
2727

28-
== 参考资料
28+
....
29+
输入:matrix = [["0"]]
30+
输出:0
31+
....
2932

30-
. {leetcode}/problems/maximal-square/solution/[Maximal Square solution - LeetCode^]
33+
*提示:*
3134

32-
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
35+
* `m == matrix.length`
36+
* `n == matrix[i].length`
37+
* `+1 <= m, n <= 300+`
38+
* `matrix[i][j]``0``1`
3339
34-
*Example:*
3540
36-
[subs="verbatim,quotes,macros"]
37-
----
38-
*Input:
39-
*
40-
1 0 1 0 0
41-
1 0 <font color="red">1 <font color="red">1 1
42-
1 1 <font color="red">1 <font color="red">1 1
43-
1 0 0 1 0
44-
45-
*Output:* 4
46-
----
41+
== 思路分析
42+
43+
根据左上、左边、上边加自身,确定一个最小正方形;然后再进一步,在小正方形基础上,从四周选择已有正方形中最小,然后扩大,再这个过程中记录下最大的正方形边长,即可得到结果。思路如下图:
44+
45+
image::images/0221-03.png[{image_attr}]
4746

47+
简化一下,不需要矩阵来存储所有正方形的计算结果,只需要一行来记录上一行计算结果和当前行计算结果即可。
48+
49+
image::images/0221-04.png[{image_attr}]
50+
51+
无需另外开辟矩阵存储中间结果,直接在参数矩阵上存储即可。
52+
53+
image::images/0221-05.png[{image_attr}]
4854

4955
[[src-0221]]
56+
[tabs]
57+
====
58+
一刷::
59+
+
60+
--
5061
[{java_src_attr}]
5162
----
5263
include::{sourcedir}/_0221_MaximalSquare.java[tag=answer]
5364
----
65+
--
66+
67+
二刷::
68+
+
69+
--
70+
[{java_src_attr}]
71+
----
72+
include::{sourcedir}/_0221_MaximalSquare_2.java[tag=answer]
73+
----
74+
--
75+
====
76+
77+
== 参考资料
5478

79+
. https://leetcode.cn/problems/maximal-square/solutions/234964/zui-da-zheng-fang-xing-by-leetcode-solution/[221. 最大正方形 - 官方题解^]

docs/images/0221-01.jpg

17.5 KB
Loading

docs/images/0221-02.jpg

3.67 KB
Loading
File renamed without changes.
File renamed without changes.

docs/images/0221-05.png

123 KB
Loading

logbook/202503.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ endif::[]
66
:doc_base_url: link:../docs
77

88

9-
[cols="7,36,7,50",options="header"]
9+
[cols="7,32,7,54",options="header"]
1010
|===
1111
|序号 |题目 |题解 |备注
1212

@@ -335,6 +335,11 @@ endif::[]
335335
|{doc_base_url}/1289-minimum-falling-path-sum-ii.adoc[题解]
336336
|⭕️ 动态规划。题目理解错误。
337337

338+
|{counter:codes2503}
339+
|{leetcode_base_url}/maximal-square/[221. 最大正方形^]
340+
|{doc_base_url}/0221-maximal-square.adoc[题解]
341+
|✅ 动态规划。直接将结果存储在参数矩阵上。如果正方形想扩大,则左边,左上和上面三个都是正方形时才可以,可以直接去这三者中的最小值。
342+
338343

339344
|===
340345

src/main/java/com/diguage/algo/leetcode/_0221_MaximalSquare.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public class _0221_MaximalSquare {
1818
* Memory Usage: 43.3 MB, less than 91.18% of Java online submissions for Maximal Square.
1919
*
2020
* Copy from: https://leetcode.com/problems/maximal-square/solution/[Maximal Square solution - LeetCode]
21+
*
22+
* @author D瓜哥 · https://www.diguage.com
23+
* @since 2020-01-28 12:21
2124
*/
2225
public int maximalSquare(char[][] matrix) {
2326
if (Objects.isNull(matrix) || matrix.length == 0) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0221_MaximalSquare_2 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-04-18 23:35:51
8+
*/
9+
public int maximalSquare(char[][] matrix) {
10+
int row = matrix.length;
11+
int col = matrix[0].length;
12+
int result = 0;
13+
for (int r = 0; r < row; r++) {
14+
for (int c = 0; c < col; c++) {
15+
if (matrix[r][c] == '0') {
16+
continue;
17+
}
18+
// 加速
19+
if (result == 0) {
20+
result = 1;
21+
}
22+
char left = '0';
23+
if (0 <= c - 1) {
24+
left = matrix[r][c - 1];
25+
}
26+
// 加速
27+
if (left == '0') {
28+
continue;
29+
}
30+
char top = '0';
31+
if (0 <= r - 1) {
32+
top = matrix[r - 1][c];
33+
}
34+
// 加速
35+
if (top == '0') {
36+
continue;
37+
}
38+
char topLeft = matrix[r - 1][c - 1];
39+
// 加速
40+
if (topLeft == '0') {
41+
continue;
42+
}
43+
matrix[r][c] = (char) (1 + Math.min(left, Math.min(topLeft, top)));
44+
result = Math.max(result, matrix[r][c] - '0');
45+
}
46+
}
47+
return result * result;
48+
}
49+
50+
// end::answer[]
51+
public static void main(String[] args) {
52+
new _0221_MaximalSquare_2().maximalSquare(new char[][]{
53+
{'1', '0', '1', '0', '0'},
54+
{'1', '0', '1', '1', '1'},
55+
{'1', '1', '1', '1', '1'},
56+
{'1', '0', '0', '1', '0'}
57+
});
58+
}
59+
}

0 commit comments

Comments
 (0)