计算每个点作为起始点时,能够得到几个矩阵。
class Solution {
public:
int func(vector<vector<int>>& matrix, int x, int y){
int row = matrix.size();
int col = matrix[0].size();
int depth = 1;
while(1){
if (x+depth-1>=row||y+depth-1>=col){
return depth-1;
}
for(int i=x;i<=x+depth-1;i++){
if (matrix[i][y+depth-1]==0){
return depth-1;
}
}
for(int j=y;j<=y+depth-1;j++){
if (matrix[x+depth-1][j]==0){
return depth-1;
}
}
depth++;
}
return depth;
}
int countSquares(vector<vector<int>>& matrix) {
int row = matrix.size();
int col = matrix[0].size();
int res = 0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
res += func(matrix, i,j);
}
}
return res;
}
};