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

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;
    }
};

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

相关推荐

  1. 2799.统计完全数目

    2024-07-16 07:48:04       33 阅读
  2. 1248.统计优美数组

    2024-07-16 07:48:04       27 阅读
  3. 2563.统计公平数目

    2024-07-16 07:48:04       33 阅读
  4. 1838.最高频元素

    2024-07-16 07:48:04       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 07:48:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 07:48:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 07:48:04       58 阅读
  4. Python语言-面向对象

    2024-07-16 07:48:04       69 阅读

热门阅读

  1. 微服务治理新篇章:Eureka中细粒度策略管理实现

    2024-07-16 07:48:04       24 阅读
  2. Lua 运算符

    2024-07-16 07:48:04       19 阅读
  3. Android 14 开机时间优化措施

    2024-07-16 07:48:04       21 阅读
  4. OpenCV 轮廓检测

    2024-07-16 07:48:04       27 阅读
  5. Jupyter Lab 使用

    2024-07-16 07:48:04       22 阅读
  6. python笔记(转存ipynb)------1

    2024-07-16 07:48:04       20 阅读
  7. 《读书笔记-骆驼祥子》

    2024-07-16 07:48:04       22 阅读
  8. sql面试题

    2024-07-16 07:48:04       21 阅读
  9. 开发指南048-mysql设置

    2024-07-16 07:48:04       26 阅读
  10. Web 中POST为什么会发送两次请求

    2024-07-16 07:48:04       24 阅读