forked from yanglei-github/Leetcode_in_python3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path221_最大正方形.py
More file actions
21 lines (20 loc) · 762 Bytes
/
221_最大正方形.py
File metadata and controls
21 lines (20 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- coding: utf-8 -*-
"""
Created on Fri May 8 09:47:27 2020
@author: leiya
"""
#矩阵中的的数据时string类型而非int类型
#dp[i][j]状态指[i,j]为右下角可以产生的最大正方形
class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
maxedge = 0
dp = [[0 for _ in range(len(matrix[0]))] for _ in range(len(matrix))]
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if matrix[i][j] == '1':
if i == 0 or j == 0:
dp[i][j] = 1
else:
dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])+1
maxedge = max(maxedge, dp[i][j])
return maxedge ** 2