1. LeetCode221題目鏈接鏈接
https://leetcode-cn.com/problems/maximal-square/submissions/
2.題目解析
該題目為在一個由 0 和 1 組成的二維矩陣內(nèi)牧抽,找到只包含 1 的最大正方形,并返回其面積。遍歷矩陣所有數(shù)據(jù)庐船,記錄1的位置能組成的矩形拭荤,記錄邊長,循環(huán)找到最大的邊長秘蛔,然后返回面積糠排。
public int maximalSquare(char[][] matrix) {
int m = matrix.length;
if(m < 1) return 0;
int n = matrix[0].length;
int max = 0;
int[][] dp = new int[m+1][n+1];
for(int i = 1; i <= m; ++i) {
for(int j = 1; j <= n; ++j) {
if(matrix[i-1][j-1] == '1') {
dp[i][j] = 1 + Math.min(dp[i-1][j-1], Math.min(dp[i-1][j], dp[i][j-1]));
max = Math.max(max, dp[i][j]);
}
}
}
return max*max;
}
3. 提交結(jié)果
提交結(jié)果