统计全为1的正方形子矩阵
0x1 题目详情
0x2 解题思路
0x3 代码实现
class Solution {
public int countSquares(int[][] matrix) {
if(matrix.length==0 || matrix[0].length==0){
return 0;
}
int[][] dp=new int[matrix.length+1][matrix[0].length+1];
int result=0;
for(int i=1;i<dp.length;i++){
for(int j=1;j<dp[0].length;j++){
if(matrix[i-1][j-1]==0){
dp[i][j]=0;
}
else{
dp[i][j]=Math.min(dp[i][j-1],Math.min(dp[i-1][j],dp[i-1][j-1]))+1;
}
if(dp[i][j]>0){
result+=dp[i][j];
}
}
}
return result;
}
}0x4 课后总结
Last updated