Skip to content

Commit 032d353

Browse files
authored
feat: 增加python 代码
1 parent 85d67d1 commit 032d353

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

problems/221.maximal-square.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,31 @@ dp[i - 1][j - 1]我们直接拿到,关键是`往上和往左进行延伸`, 最
5555

5656
## 代码
5757

58-
```js
58+
代码支持:Python,JavaScript:
59+
60+
Python Code:
61+
62+
```python
63+
class Solution:
64+
def maximalSquare(self, matrix: List[List[str]]) -> int:
65+
res = 0
66+
m = len(matrix)
67+
if m == 0:
68+
return 0
69+
n = len(matrix[0])
70+
dp = [[0] * (n + 1) for _ in range(m + 1)]
71+
72+
for i in range(1, m + 1):
73+
for j in range(1, n + 1):
74+
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1 if matrix[i - 1][j - 1] == "1" else 0
75+
res = max(res, dp[i][j])
76+
return res ** 2
77+
```
5978

6079

80+
JavaScript Code:
81+
82+
```js
6183

6284
/*
6385
* @lc app=leetcode id=221 lang=javascript
@@ -98,7 +120,8 @@ var maximalSquare = function(matrix) {
98120
};
99121
```
100122

123+
101124
***复杂度分析***
102-
- 时间复杂度:$O(M * N)$,其中M为行数,N为列数。
103-
- 空间复杂度:$O(N)$,其中N为列数。
104125

126+
- 时间复杂度:$O(M * N)$,其中M为行数,N为列数。
127+
- 空间复杂度:$O(M * N)$,其中M为行数,N为列数。

0 commit comments

Comments
 (0)