元素和小于k的子矩阵数目

https://leetcode.cn/problems/count-submatrices-with-top-left-element-and-sum-less-than-k/description/

int countSubmatrices(int** grid, int gridSize, int* gridColSize, int k) {
int x=gridSize,y=*gridColSize;
int a[x][y];
memset(a,0,sizeof(a));
int count = 0;
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
if(i0){
if(j
0)a[i][j]=grid[i][j];
else a[i][j]=a[i][j-1]+grid[i][j];
}
else{
if(j==0)a[i][j]=a[i-1][j]+grid[i][j];
else a[i][j]=a[i-1][j]+a[i][j-1]+grid[i][j]-a[i-1][j-1];
}
if(a[i][j]>k)break;
count++;
}
}
return count;
}

posted @ 2024-11-20 22:24  BiankaShenhen  阅读(8)  评论(0编辑  收藏  举报