class Solution(object):
def maximalSquare(self, matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
#dp[i][j]存储的是右下角坐标[i,j]的最大边长
if matrix == []:
return 0
m = len(matrix)
n = len(matrix[0])
maxlen = 0
dp = [[0 for i in range(n)] for j in range(m)]
for i in range(m):
dp[i][0] = int(matrix[i][0])
if dp[i][0] == 1:
maxlen =1
for j in range(1,n):
dp[0][j] = int(matrix[0][j])
if dp[0][j] == 1:
maxlen =1
for i in range(1,m):
for j in range(1,n):
if matrix[i][j] == "1":
dp[i][j] = min(dp[i-1][j-1]+1,dp[i][j-1]+1,dp[i-1][j]+1)
maxlen = max(maxlen, dp[i][j])
return maxlen**2
python leetcode 221. Maximal Square
最新推荐文章于 2022-01-18 12:53:37 发布