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. Python 的元类设计起源自哪里?

    一个元老级的 Python 核心开发者曾建议我们( 点击阅读),应该广泛学习其它编程语言的优秀特性,从而提升 Python 在相关领域的能力.在关于元编程方面,他的建议是学习 Hy 和 Ruby.但是 ...

  2. 多线程多进程学习threading,queue线程安全队列,线程间数据状态读取。threading.local() threading.RLock()

    http://www.cnblogs.com/alex3714/articles/5230609.html python的多线程是通过上下文切换实现的,只能利用一核CPU,不适合CPU密集操作型任务, ...

  3. FlashFXP链接到服务器上,如果www目录下的文件隐藏

    FlashFXP链接到服务器上,如果www目录下的文件隐藏,那么请按照如下设置,就可以显示隐藏的文件了 [站点]->[站点管理器]->选项,然后按照如下设置:

  4. JAVA上传文件到FTP上

    添加maven <!-- https://mvnrepository.com/artifact/commons-net/commons-net --> <dependency> ...

  5. C语言补漏-逗号运算符与逗号表达式

    1. 新名词? 今天看到一个新名词: 逗号表达式. C语言就有.额,怎么当时学习C没见过,一头雾水. 对我来说是新名词,其实它早就存在了,只是我还不知道. 2. 逗号表达式 C语言提供了逗号运算符-- ...

  6. Linux蓝牙库blueZ

    1.blueZ 官网 2.zigbee 网络库zigbee(可用在smart home devices)

  7. 【linux项目】lichee nano linux烧写

    目录 前言 参考: 安装交叉编译链 搭建 SPI FLASH 烧录环境 让芯片进入烧写模式 sunxi 烧写命令 u-boot 裁剪 拉取 u-boot 源码 配置 u-boot 检查 flash 驱 ...

  8. 【LeetCode】362. Design Hit Counter 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  9. 【LeetCode】58. Length of Last Word 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...

  10. A. Lorenzo Von Matterhorn

    A. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input sta ...