动态规划,dp[i][j]=min(dp[i-1][j-1],L[i-1][j],up[i][j-1])+1;【如果不存在则dp,L,up为0】
dp:此位置最大正方形边长,L此位置向左延申1的最长长度,up此位置向上延申1的最长长度
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int n = matrix.size(), m = n ? matrix[0].size() : 0, ans = 0;
vector<vector<int>> L(n, vector<int>(m, 0)), up(n, vector<int>(m, 0)), dp(n, vector<int>(m, 0));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j)
if (matrix[i][j] == '1') {
if (i > 0) dp[i][j] = up[i - 1][j], up[i][j] = up[i - 1][j];
if (j > 0) dp[i][j] = min(dp[i][j], L[i][j - 1]), L[i][j] = L[i][j - 1];
else dp[i][j] = 0;
if (i > 0 && j > 0) dp[i][j] = min(dp[i - 1][j - 1], dp[i][j]);
++L[i][j], ++up[i][j];
ans = max(ans, ++dp[i][j]);
}
}
return ans * ans;
}
};