我正在尝试解决一个问题,我必须在矩阵内找到1的最大平方,只包含零和1。
完整陈述在此处:https://leetcode.com/problems/maximal-square/description/
我的解决方案是:
def maximalSquare(matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
if len(matrix)==0:
return 0
maxlen = 0
dp = [[0 for x in range(len(matrix[0])+1)] for x in range(len(matrix)+1)]
for i in range(1, len(matrix)+1):
for j in range(1, len(matrix[0])+1):
if matrix[i-1][j-1]==1:
dp[i][j] = min(dp[i-1][j], dp[i-1][j-1], dp[i][j-1]) + 1
maxlen = max(dp[i][j], maxlen)
return maxlen
示例运行:
Your input
[["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
Your answer
0
Expected answer
4
现在,我的逻辑很简单:如果有三个排列成L形,第四个表示存在一个正方形。
但答案很多,我无法找到的错误是什么。
答案 0 :(得分:2)
由于矩阵包含字符串,因此比较应为
matrix[i-1][j-1]=="1"
而不是
matrix[i-1][j-1]==1