当前位置: 首页 > news >正文

leetcode0304. 二维区域和检索 - 矩阵不可变

1 题目: 二维区域和检索 - 矩阵不可变

官方标定难度:中

给定一个二维矩阵 matrix,以下类型的多个请求:

计算其子矩形范围内元素的总和,该子矩阵的 左上角 为 (row1, col1) ,右下角 为 (row2, col2) 。
实现 NumMatrix 类:

NumMatrix(int[][] matrix) 给定整数矩阵 matrix 进行初始化
int sumRegion(int row1, int col1, int row2, int col2) 返回 左上角 (row1, col1) 、右下角 (row2, col2) 所描述的子矩阵的元素 总和 。

示例 1:

在这里插入图片描述

输入:
[“NumMatrix”,“sumRegion”,“sumRegion”,“sumRegion”]
[[[[3,0,1,4,2],[5,6,3,2,1],[1,2,0,1,5],[4,1,0,1,7],[1,0,3,0,5]]],[2,1,4,3],[1,1,2,2],[1,2,2,4]]
输出:
[null, 8, 11, 12]

解释:
NumMatrix numMatrix = new NumMatrix([[3,0,1,4,2],[5,6,3,2,1],[1,2,0,1,5],[4,1,0,1,7],[1,0,3,0,5]]);
numMatrix.sumRegion(2, 1, 4, 3); // return 8 (红色矩形框的元素总和)
numMatrix.sumRegion(1, 1, 2, 2); // return 11 (绿色矩形框的元素总和)
numMatrix.sumRegion(1, 2, 2, 4); // return 12 (蓝色矩形框的元素总和)

提示:

m == matrix.length
n == matrix[i].length
1 <= m, n <= 200
− 1 0 5 -10^5 105 <= matrix[i][j] <= 1 0 5 10^5 105
0 <= row1 <= row2 < m
0 <= col1 <= col2 < n
最多调用 1 0 4 10^4 104次 sumRegion 方法

2 solution

本题还是反复计算区间和,不过是二维区域的,做法基本一样。计算前缀和。每一个位置保存该位置左上角所有数的和。然后用三个区域就可以拼接出任意矩形区域的和

代码


class NumMatrix {
public:
    vector<vector<int>> *mat;

    NumMatrix(vector<vector<int>> &matrix) {
        mat = new vector<vector<int>>(matrix.size() + 1, vector<int>(matrix[0].size() + 1, 0));
        for (int i = 0; i < matrix.size(); i++) {
            for (int j = 0; j < matrix[0].size(); j++) {
                (*mat)[i + 1][j + 1] =
                        (*mat)[i + 1][j] + (*mat)[i][j + 1] + matrix[i][j] - (*mat)[i][j];
            }
        }
    }

    int sumRegion(int row1, int col1, int row2, int col2) {
        return (*mat)[row2 + 1][col2 + 1] + (*mat)[row1][col1]
               - (*mat)[row2 + 1][col1] - (*mat)[row1][col2 + 1];
    }  
};

结果

在这里插入图片描述


文章转载自:

http://CE0QpBOz.sfsjh.cn
http://9WOegpTu.sfsjh.cn
http://ZoEs99LT.sfsjh.cn
http://7C0viv3W.sfsjh.cn
http://DupiptVA.sfsjh.cn
http://CD6Be8cn.sfsjh.cn
http://ifHtxMTO.sfsjh.cn
http://ywy4XtgE.sfsjh.cn
http://j9noChs0.sfsjh.cn
http://fVF5fZry.sfsjh.cn
http://hsErDijR.sfsjh.cn
http://EtcbVWVJ.sfsjh.cn
http://QGHptOA6.sfsjh.cn
http://W6qp2B2K.sfsjh.cn
http://hvZRuAu0.sfsjh.cn
http://YhmKPvgP.sfsjh.cn
http://muAdmIWW.sfsjh.cn
http://AK0W7B2v.sfsjh.cn
http://ee5uxanQ.sfsjh.cn
http://Rtyo4pNl.sfsjh.cn
http://z610Z4vk.sfsjh.cn
http://QLg8HWEy.sfsjh.cn
http://ksi3SDcW.sfsjh.cn
http://UKM2CqId.sfsjh.cn
http://YkGU4BvN.sfsjh.cn
http://udOB8Ti3.sfsjh.cn
http://yv53YNdS.sfsjh.cn
http://twVi8YYE.sfsjh.cn
http://1PGcybYg.sfsjh.cn
http://z2gsBuwa.sfsjh.cn
http://www.dtcms.com/a/89891.html

相关文章:

  • 新一代可编程网关应用举例
  • 第一章:优化概述_《C++性能优化指南》notes
  • 权限提升—Windows权限提升土豆家族溢出漏洞通杀全系
  • uni-app自动升级功能
  • spring boot jwt生成token
  • OpenBMC:BmcWeb添加路由5 设置handler函数
  • 网络华为HCIA+HCIP 动态路由协议
  • 大模型训练 | 智能体知识库 资源收集之心理咨询问答数据集
  • Sqoop-试题
  • mysql的学习
  • C语言的内存模型 (堆区,栈区,静态区,常量区,代码区 )概念讲解
  • 互感器制作流程
  • 什么是独立服务器?为什么选择它?
  • 数据分析中,文件解析库解析内容样式调整
  • 一个数组分为两个sum相等的数组
  • 正弦函数的连续傅里叶变换正弦序列的DTFT
  • FPGA助力智能机器人应用
  • 小样本学习(Few-Shot Learning)基本概念 VS 监督学习
  • docker-操作实战
  • 为什么递归用栈?动态分配用堆?
  • 网络编程的概念&作用
  • vscode ssh连接ubantu显示管道不存在,VMware Virtual Ethernet Adapter for VMnet8不存在
  • 6.3 模拟专题:LeetCode 6. Z 字形变换
  • Vue3 知识点总结
  • 在 PostgreSQL 中设置调试环境以更好地理解 OpenSSL API
  • DeepSeek 助力 Vue3 开发:打造丝滑的表格(Table)之添加行拖拽排序功能示例1,TableView16_01.vue 基础行拖拽排序示例
  • cnn中的dropout技术
  • 如何在jupyter notebook中使用django框架
  • Linux 配置时间服务器
  • 企业级全栈开发终极指南:Spring Boot+Vue3+Kubernetes实战,从0到上线高并发系统