动态规划解法
先建一个二维数组,该二维数组存储的是边长,边长指的是该位置的正方形的边长,然后再里面找最大的边长,输出边长*边长
C++
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int row = matrix.size();
if(row==0){
return 0;
}
int col = matrix[0].size();
vector<vector<int>>temp(row, vector<int>(col,0));
int res_max=0;
for(int i=0;i<row;i++){
temp[i][0]=matrix[i][0]-'0';
res_max = max(res_max, temp[i][0]);
}
for(int j=0;j<col;j++){
temp[0][j]=matrix[0][j]-'0';
res_max = max(res_max, temp[0][j]);
}
// cout<<res_max;
for(int i=1;i<row;i++){
for(int j=1;j<col;j++){
if(matrix[i][j]=='1'){
temp[i][j]=min(temp[i-1][j],min(temp[i][j-1],temp[i-1][j-1]))+1;
res_max = max(res_max, temp[i][j]);
}else{
continue;
}
}
}
return res_max*res_max;
}
};