标签:alt 思路 color rgb 最小值 矩阵 mamicode mat matrix
在一个由 ‘0‘
和 ‘1‘
组成的二维矩阵内,找到只包含 ‘1‘
的最大正方形,并返回其面积。
输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
输出:4
class Solution {
public int maximalSquare(char[][] matrix) {
//思路:对矩阵当中的位置i,j,dp[i,j]代表以该点作为正方形的右下角时正方形的最大边长
//该点由其左角、上角和左上角共同决定,取其三角的最小值+1,即是该点的dp值
int ans = 0, n = matrix.length, m = matrix[0].length;
int[][] dp = new int[n+1][m+1];
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= m; ++j)
{
if(matrix[i-1][j-1] == ‘1‘)
dp[i][j] = Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1])+1;
ans = Math.max(ans, dp[i][j]);
}
}
return ans*ans;
}
}
标签:alt 思路 color rgb 最小值 矩阵 mamicode mat matrix
原文地址:https://www.cnblogs.com/pionice/p/14626155.html