leetcode221.最大正方形 地址: https://leetcode-cn.com/problems/maximal-square/ python代码 class Solution: def maximalSquare(self, matrix) : row = len(matrix) col = len(matrix[0]) res = [[0 for _ in range(col)] for _ in range(row)] ans = 0 for i in range(row): for j in range(col): if matrix[i][j]=='1': if 0<=i-1<row and 0<=j-1<col: if res[i-1][j]==res[i][j-1]: if matrix[i-1-res[i-1][j]+1][j-res[i-1][j]] == '1': res[i][j] = res[i-1][j]+1 else: res[i][j] = res[i - 1][j] else: res[i][j] = min(res[i-1][j],res[i][j-1])+1 else: res[i][j] +=1 print(res,max(res),max(max(res))) # return np.max(res)**2 # return max(map(max, res))**2 return max(max(row) for row in res)**2 思路 动态规划,利用res[i-1][j]和res[i][j-1]确定res[i][j]趣点 二维list求最大值 利用map,max(map(max, res))按列循环求解,max(max(row) for rol in res), 相比1速度更快,内存更小利用numpy, np.max(res),在leetcode上通不过就不测了