LeetCode刷题实战221:最大正方形
程序IT圈
共 2194字,需浏览 5分钟
·
2021-03-27 13:58
Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
示例
解题
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
//dp[i][j]表示以(i,j)为右下角能构成的最大正方形边长
int m=matrix.size();
if(m<1)
return 0;
int n=matrix[0].size();
vector<vector<int>> dp(m+1,vector<int>(n+1,0));
int imax=0;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
if(matrix[i-1][j-1]=='1')
dp[i][j]=min(min(dp[i-1][j-1],dp[i-1][j]),dp[i][j-1])+1;
imax=max(imax,dp[i][j]);
}
return imax*imax;
}
};
评论