|
42 | 42 | <li><code>matrix[i][j]</code> 为 <code>'0'</code> 或 <code>'1'</code></li>
|
43 | 43 | </ul>
|
44 | 44 |
|
45 |
| - |
46 | 45 | ## 解法
|
47 | 46 |
|
48 | 47 | <!-- 这里可写通用的实现逻辑 -->
|
49 | 48 |
|
| 49 | +动态规划。 |
| 50 | + |
| 51 | +设 `dp[i + 1][j + 1]` 表示以下标 `(i, j)` 作为正方形右下角的最大正方形边长。 |
| 52 | + |
| 53 | +当 `matrix[i][j] == '1'`, `dp[i + 1][j + 1] = min(dp[i][j + 1], dp[i + 1][j], dp[i][j]) + 1`,否则 `dp[i + 1][j + 1] = 0`。 |
| 54 | + |
50 | 55 | <!-- tabs:start -->
|
51 | 56 |
|
52 | 57 | ### **Python3**
|
53 | 58 |
|
54 | 59 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
55 | 60 |
|
56 | 61 | ```python
|
57 |
| - |
| 62 | +class Solution: |
| 63 | + def maximalSquare(self, matrix: List[List[str]]) -> int: |
| 64 | + m, n = len(matrix), len(matrix[0]) |
| 65 | + dp = [[0] * (n + 1) for _ in range(m + 1)] |
| 66 | + mx = 0 |
| 67 | + for i in range(m): |
| 68 | + for j in range(n): |
| 69 | + if matrix[i][j] == '1': |
| 70 | + dp[i + 1][j + 1] = min(dp[i][j + 1], dp[i + 1][j], dp[i][j]) + 1 |
| 71 | + mx = max(mx, dp[i + 1][j + 1]) |
| 72 | + return mx * mx |
58 | 73 | ```
|
59 | 74 |
|
60 | 75 | ### **Java**
|
61 | 76 |
|
62 | 77 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
63 | 78 |
|
64 | 79 | ```java
|
| 80 | +class Solution { |
| 81 | + public int maximalSquare(char[][] matrix) { |
| 82 | + int m = matrix.length, n = matrix[0].length; |
| 83 | + int[][] dp = new int[m + 1][n + 1]; |
| 84 | + int mx = 0; |
| 85 | + for (int i = 0; i < m; ++i) { |
| 86 | + for (int j = 0; j < n; ++j) { |
| 87 | + if (matrix[i][j] == '1') { |
| 88 | + dp[i + 1][j + 1] = Math.min(Math.min(dp[i][j + 1], dp[i + 1][j]), dp[i][j]) + 1; |
| 89 | + mx = Math.max(mx, dp[i + 1][j + 1]); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + return mx * mx; |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### **C++** |
| 99 | + |
| 100 | +```cpp |
| 101 | +class Solution { |
| 102 | +public: |
| 103 | + int maximalSquare(vector<vector<char>>& matrix) { |
| 104 | + int m = matrix.size(), n = matrix[0].size(); |
| 105 | + vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0)); |
| 106 | + int mx = 0; |
| 107 | + for (int i = 0; i < m; ++i) { |
| 108 | + for (int j = 0; j < n; ++j) { |
| 109 | + if (matrix[i][j] == '1') { |
| 110 | + dp[i + 1][j + 1] = min(min(dp[i][j + 1], dp[i + 1][j]), dp[i][j]) + 1; |
| 111 | + mx = max(mx, dp[i + 1][j + 1]); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + return mx * mx; |
| 116 | + } |
| 117 | +}; |
| 118 | +``` |
| 119 | +
|
| 120 | +### **Go** |
| 121 | +
|
| 122 | +```go |
| 123 | +func maximalSquare(matrix [][]byte) int { |
| 124 | + m, n := len(matrix), len(matrix[0]) |
| 125 | + dp := make([][]int, m+1) |
| 126 | + for i := 0; i <= m; i++ { |
| 127 | + dp[i] = make([]int, n+1) |
| 128 | + } |
| 129 | + mx := 0 |
| 130 | + for i := 0; i < m; i++ { |
| 131 | + for j := 0; j < n; j++ { |
| 132 | + if matrix[i][j] == '1' { |
| 133 | + dp[i+1][j+1] = min(min(dp[i][j+1], dp[i+1][j]), dp[i][j]) + 1 |
| 134 | + mx = max(mx, dp[i+1][j+1]) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + return mx * mx |
| 139 | +} |
| 140 | +
|
| 141 | +func max(a, b int) int { |
| 142 | + if a > b { |
| 143 | + return a |
| 144 | + } |
| 145 | + return b |
| 146 | +} |
| 147 | +
|
| 148 | +func min(a, b int) int { |
| 149 | + if a < b { |
| 150 | + return a |
| 151 | + } |
| 152 | + return b |
| 153 | +} |
| 154 | +``` |
65 | 155 |
|
| 156 | +### **C#** |
| 157 | + |
| 158 | +```cs |
| 159 | +public class Solution { |
| 160 | + public int MaximalSquare(char[][] matrix) { |
| 161 | + int m = matrix.Length, n = matrix[0].Length; |
| 162 | + var dp = new int[m + 1, n + 1]; |
| 163 | + int mx = 0; |
| 164 | + for (int i = 0; i < m; ++i) |
| 165 | + { |
| 166 | + for (int j = 0; j < n; ++j) |
| 167 | + { |
| 168 | + if (matrix[i][j] == '1') |
| 169 | + { |
| 170 | + dp[i + 1, j + 1] = Math.Min(Math.Min(dp[i, j + 1], dp[i + 1, j]), dp[i, j]) + 1; |
| 171 | + mx = Math.Max(mx, dp[i + 1, j + 1]); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + return mx * mx; |
| 176 | + } |
| 177 | +} |
66 | 178 | ```
|
67 | 179 |
|
68 | 180 | ### **...**
|
|
0 commit comments