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).

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.

前面一个博文的衍生版本,由原来的一维矩阵变成了现在的二维矩阵,没什么区别,还是先建立和的数组,代码如下所示:

 class NumMatrix {
public:
NumMatrix(vector<vector<int>> &matrix){
if(!matrix.size() || !matrix[].size())
return;
sum = vector<vector<int>>(matrix.size(), vector<int>(matrix[].size(), ));
for(int i = ; i < matrix.size(); ++i){
for(int j = ; j < matrix[].size(); ++j){
if(i != && j != ){
sum[i][j] = matrix[i][j] + sum[i][j-] + sum[i-][j] - sum[i-][j-];
}else if(i == && j == ){
sum[i][j] = matrix[i][j];
}else if(i == ){
sum[i][j] = matrix[i][j] + sum[i][j-];
}else{
sum[i][j] = matrix[i][j] + sum[i-][j];
}
}
}
} int sumRegion(int row1, int col1, int row2, int col2) {
if(row1 == && col1 == )
return sum[row2][col2];
else if(row1 == )
return sum[row2][col2] - sum[row2][col1-];
else if(col1 == )
return sum[row2][col2] - sum[row1-][col2];
else
return sum[row2][col2] - sum[row2][col1-] - sum[row1-][col2] + sum[row1-][col1-]; }
private:
vector<vector<int>> sum;
};

LeetCode OJ:Range Sum Query 2D - Immutable(区域和2D版本)的更多相关文章

  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] 303. Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  3. [LeetCode] 307. Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  4. LeetCode 308. Range Sum Query 2D - Mutable

    原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...

  5. [LeetCode] 303. Range Sum Query - Immutable (Easy)

    303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...

  6. [Leetcode Week16]Range Sum Query - Mutable

    Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. iPhone X 游戏闪退:NSUnknownKeyException

    目前很多游戏在iPhone X手机 wifi情况下,启动时候闪退,在4G网络时候不闪退. 闪退的log: #0 Thread NSUnknownKeyException [<UIStatusBa ...

  2. ES6 利用 Set 数组去重法

    例子: const set = new Set(); [2, 3, 5, 4, 5, 2, 2].forEach(x => set.add(x) ); const arr = [...set]; ...

  3. get请求参数为中文,参数到后台出现乱码(注:乱码情况千奇百怪,这里贴我遇到的情况)

    前言 get请求的接口从页面到controller类出现了乱码. 解决 参数乱码: String param = "..."; 使用new String(param.getByte ...

  4. Java硬件同步机制Swap指令模拟+记录型信号量模拟

    学校实验存档//.. 以经典的生产者消费者问题作为背景. 进程同步方式接口: package method; /** * P表示通过,V表示释放 */ public interface Method ...

  5. tomcat结合memcached构建session服务器

    memcached服务器两台:192.168.223.136,192.168.223.137 tomcat多实例:192.168.233.146:8081,192.168.223.146:8082 f ...

  6. java处理json的工具类(list,map和json的之间的转换)

    需要下载第三方的jar :net.sf.json import java.io.BufferedReader; import java.io.InputStream; import java.io.I ...

  7. wechat4j获取用户昵称乱码修复

    项目对接微信公众号平台时,微信的官方给出的建议是使用wechat4j.官方建议的,自然心里踏实,但实际用起来时发现wechat4j埋有很多雷,最让人心烦意乱的就是中文乱码问题. 之前写过一篇为JAXB ...

  8. MongoDB 性能优化

    Read Preferences/读写分离 有时候为了考虑应用程序的性能或响应性,为了提高读取操作的吞吐率,一个常见的措施就是进行读写分离,MongoDB副本集对读写分离的支持是通过Read Pref ...

  9. java代码实现JVM栈溢出,堆溢出

    参考博客:http://www.cnblogs.com/tv151579/p/3647238.html 背景知识: 栈存放什么:栈存储运行时声明的变量——对象引用(或基础类型, primitive)内 ...

  10. input输入框延时发送请求问题

    同样是面试遇到的问题,input输入框,怎么减少发送请求次数. 键盘抬起触发事件,首先清除定时器,再开启定时器.只要小于1s的连续输入,都会先把上一次定时器清除.停顿一秒后,开始执行请求事件(此处为c ...