3212力扣:统计X和Y频数相等的子矩阵数量

cpp 复制代码
class Solution {
public:
    int numberOfSubmatrices(vector<vector<char>>& grid) {
        int n=grid.size();
        int m=grid[0].size();
        vector<vector<int>> prex(n+1,vector<int>(m+1,0));
        vector<vector<int>> prey(n+1,vector<int>(m+1,0));
        for(int i=1;i<n+1;i++){
            for(int j=1;j<m+1;j++){
                if(grid[i-1][j-1]=='X'){
                    prex[i][j]=prex[i-1][j]+prex[i][j-1]-prex[i-1][j-1]+1;}
                else{
                    prex[i][j]=prex[i-1][j]+prex[i][j-1]-prex[i-1][j-1]+0;
                }
                if(grid[i-1][j-1]=='Y'){
                    prey[i][j]=prey[i-1][j]+prey[i][j-1]-prey[i-1][j-1]+1;
                }else{
                    prey[i][j]=prey[i-1][j]+prey[i][j-1]-prey[i-1][j-1]+0;
                }
            }
        }
        int res=0;
        for(int i=1;i<n+1;i++){
            for(int j=1;j<m+1;j++){
                int countx=prex[i][j]-prex[0][j]-prex[i][0]+prex[0][0];
                int county=prey[i][j]-prey[0][j]-prey[i][0]+prey[0][0];
                if(countx==county && countx>0){res++;}
            }
        }
        return res;
    }
};

二维前缀和 前缀和数组定义时扩宽一层

相关推荐
uhakadotcom9 分钟前
Google Cloud Dataproc:简化大数据处理的强大工具
后端·算法·面试
uhakadotcom1 小时前
PyTorch 分布式训练入门指南
算法·面试·github
uhakadotcom1 小时前
PyTorch 与 Amazon SageMaker 配合使用:基础知识与实践
算法·面试·github
uhakadotcom1 小时前
在Google Cloud上使用PyTorch:如何在Vertex AI上训练和调优PyTorch模型
算法·面试·github
wen__xvn1 小时前
c++STL入门
开发语言·c++·算法
呵呵哒( ̄▽ ̄)"2 小时前
常考题:通过解方程组求矩阵
线性代数·矩阵
weisian1514 小时前
力扣经典算法篇-9-跳跃游戏(贪心算法,反向递推)
算法·leetcode·游戏
MCYH02064 小时前
C++抽卡模拟器
java·c++·算法·概率·原神
pystraf4 小时前
P10587 「ALFR Round 2」C 小 Y 的数 Solution
数据结构·c++·算法·线段树·洛谷
ゞ 正在缓冲99%…4 小时前
leetcode221.最大正方形
java·算法·动态规划