Range Sum Query - Immutable

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

Example:

Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3

Note:

  1. You may assume that the array does not change.
  2. There are many calls to sumRange function.
 class NumArray {
private:
vector<int> acc; public:
NumArray(vector<int> &nums) {
acc.push_back();
for (auto n : nums) {
acc.push_back(acc.back() + n);
}
} int sumRange(int i, int j) {
return acc[j + ] - acc[i];
}
}; // Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);

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.
 class NumMatrix {
private:
vector<vector<int>> acc;
public:
NumMatrix(vector<vector<int>> &matrix) {
if (matrix.empty()) return;
int n = matrix.size(), m = matrix[].size();
acc.resize(n + , vector<int>(m + ));
for (int i = ; i <= n; ++i) acc[i][] = ;
for (int j = ; j <= m; ++j) acc[][j] = ;
for (int i = ; i <= n; ++i) {
for (int j = ; j <= m; ++j) {
acc[i][j] = acc[i][j-] + acc[i-][j] - acc[i-][j-] + matrix[i-][j-];
}
}
} int sumRegion(int row1, int col1, int row2, int col2) {
return acc[row2+][col2+] - acc[row1][col2+] - acc[row2+][col1] + acc[row1][col1];
}
}; // Your NumMatrix object will be instantiated and called as such:
// NumMatrix numMatrix(matrix);
// numMatrix.sumRegion(0, 1, 2, 3);
// numMatrix.sumRegion(1, 2, 3, 4);

[LeetCode] Range Sum Query - Immutable & Range Sum Query 2D - Immutable的更多相关文章

  1. [LeetCode] 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 解题报告(Python)

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

  3. 【刷题-LeetCode】304. Range Sum Query 2D - Immutable

    Range Sum Query 2D - Immutable Given a 2D matrix matrix, find the sum of the elements inside the rec ...

  4. LeetCode OJ:Range Sum Query 2D - Immutable(区域和2D版本)

    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

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

  6. 304. Range Sum Query 2D - Immutable

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

  7. LeetCode-304. Range Sum Query 2D - Immutable

    Description: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by ...

  8. LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$

    Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...

  9. [LeetCode] 548. Split Array with Equal Sum 分割数组成和相同的子数组

    Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...

随机推荐

  1. 2287: 【POJ Challenge】消失之物

    Description ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. "要使用剩下的 N - 1 物品装满容积为 x ...

  2. MySQL MHA配置

    MySQL环境: master:192.168.202.129:3306 slave:192.168.202.129:3307,192.168.202.129:3307,192.168.202.130 ...

  3. 3D扫描系统的构建(待处理)

    1. http://www.zhihu.com/question/32143353 是否可以 DIY 一个 3D 扫描仪或者开源 3D 扫描项目? 详细的原理介绍 2. http://www.csks ...

  4. PHP--Warning: Invalid argument supplied for foreach() in ...

    1.背景 今天学习PHPExcel的使用,在代码执行foreach($data as $value){...}的时候出现这样一个警告提示:Warning: Invalid argument suppl ...

  5. Linq学习之操作符

    一.环境搭建 下面将逐步搭建我们学习的环境,这个环境不仅仅是这次需要使用,以后的教程一样需要使用这个环境.所以请大家务必按照 搭建这里的环境否则会影响你后面的学习. 我们用到的几张表 通知消息表: 用 ...

  6. fiddler 无法捕获apache httpclient报文的问题及解决

    问题如题,解决办法为在构建httpclient对象的时候设置代理,因为fiddler内置了一个代理,只有流量(traffic)经过这个代理,才能够被捕捉到. HttpHost proxy = ); C ...

  7. 集群NAS技术架构

    http://blog.csdn.net/liuaigui/article/details/6422700

  8. 专题:点滴Javascript

    JS#38: Javascript中递归造成的堆栈溢出及解决方案 JS#37: 使用console.time测试Javascript性能 JS#36: Javascript中判断两个日期相等 JS#3 ...

  9. spring定时任务轮询(spring Task)

    定时任务轮询比如任务自服务器启动就开始运行,并且每隔5秒执行一次. 以下用spring注解配置定时任务.1.添加相应的schema xmlns:task=" xsi:schemaLocati ...

  10. mysql导入sql文件

    从命令提示符下到MYSQL文件目录中的Bin文件夹下,执行命令 mysql -u root -p databasename < db.sql 其中root是你MYSQL的用户名,database ...