1. Range Sum Query 2D - Immutable

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).



The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.

Example:

Given matrix = [
[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]
] sumRegion(2, 1, 4, 3) -> 8
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12

Note:

  1. You may assume that the matrix does not change.
  2. There are many calls to sumRegion function.
  3. You may assume that row1 ≤ row2 and col1 ≤ col2.

同一维数组一样,先预处理

定义数组\(\mathrm{S}[i][j]\)表示前 i-1 行前 j-1 列交叉区域的和

  • 预处理阶段:\(\mathrm{S}[i][j] =\mathrm{M}[i-1][j-1]\mathrm{S}[i][j-1]+\mathrm{S}[i-1][j] - \mathrm{S}[i-1][j-1]\)

  • 查询阶段:\(\mathrm{sum\_of\_region}[r1, c1, r2, c2] = \mathrm{S}[r2+1][c2+1]-\mathrm{S}[r1][c2+1]-\mathrm{S}[r2+1][c1]+\mathrm{S}[r1][c1]\)

class NumMatrix {
public:
vector<vector<int>>S;
NumMatrix(vector<vector<int>>& matrix) {
int m = matrix.size();
if(m > 0){
int n = matrix[0].size();
S.resize(m+1, vector<int>(n+1, 0));
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
S[i+1][j+1] = matrix[i][j] + S[i][j+1]+S[i+1][j] - S[i][j];
}
}
}
} int sumRegion(int row1, int col1, int row2, int col2) {
if(S.size() == 0)return 0;
return S[row2+1][col2+1] - S[row1][col2+1] - S[row2+1][col1] + S[row1][col1];
}
};

【刷题-LeetCode】304. Range Sum Query 2D - Immutable的更多相关文章

  1. [LeetCode] 304. Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  2. [leetcode]304. Range Sum Query 2D - Immutable二维区间求和 - 不变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  3. leetcode 304. Range Sum Query 2D - Immutable(递推)

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  4. LeetCode 304. Range Sum Query 2D – Immutable

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  5. LeetCode 304. Range Sum Query 2D - Immutable 二维区域和检索 - 矩阵不可变(C++/Java)

    题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...

  6. 【LeetCode】304. Range Sum Query 2D - Immutable 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 预先求和 相似题目 参考资料 日期 题目地址:htt ...

  7. 304. Range Sum Query 2D - Immutable

    题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...

  8. 304. Range Sum Query 2D - Immutable(动态规划)

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  9. 304 Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...

随机推荐

  1. CF908A New Year and Counting Cards 题解

    Content 有 \(n\) 张卡牌,每张卡牌上只会有大小写字母和 \(0\sim 9\) 的阿拉伯数字.有这样一个描述:"如果卡牌正面写有元音字母(\(\texttt{A,E,I,O,U ...

  2. CPU中断数查看与网卡中断绑核

    CPU中断数查看 多核CPU每个核心CPU发生中断的数量查看 # mpstat -I SUM -P ALL 1 3 Linux 5.4.0-40-generic (verify-new-511kern ...

  3. office2007(word2007)另存为pdf文档

    默认office2007(word2007)是没有另存为pdf文档的功能的,需要安装插件 插件地址:https://yvioo.lanzous.com/iO5myedoceh 下载之后直接安装,安装成 ...

  4. 【LeetCode】888. Fair Candy Swap 公平的糖果棒交换(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人公众号: 每日算法题 本文关键词:力扣,LeetCode,算法题,算法,Python 目录 题目描述 题目大意 解题方法 代码 刷题心得 关于作 ...

  5. 【LeetCode】468. Validate IP Address 解题报告(Python)

    [LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  6. hdu-3183A Magic Lamp(贪心)

    题目的意思是: 给你一个大数,然后删减其中的K个数,并且剩下的数还是按原来在的先后次序排列,求所得的那个数最小的那个数. 思路:贪心(要取得数最小,你从左往右选数的时候,选的第一数,就是选后组成数的位 ...

  7. 山寨F407板子如何烧录正点原子例程、Keil下载提示Invalid ROM Table

    山寨F407板子如何烧录正点原子例程 手头这块块信赢达的STM32F407板子,是模仿正点原子探索者STM32F407板子的,外设和接口,几乎是一模一样. 探索者板子用的是STM32F407ZET6, ...

  8. matplotlib 进阶之Constrained Layout Guide

    目录 简单的例子 Colorbars Suptitle Legends Padding and Spacing spacing with colobars rcParams Use with Grid ...

  9. 新环境chart包helmlint校验

    在iot目录内可以执行helm lint iot-api 去校验

  10. 记一次log4j2引发的渗透测试

    前言 记一次log4j2打入内网并用CVE-2021-42287.CVE-2021-42278获取到DC权限的靶场渗透. 外网打点 首先对web进行端口扫描,发现38080端口和22端口 访问一下38 ...