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. PHP ftp使用

    本文章来总结几个利用php中的ftp功能来实现通过FTP上传文件,有需要学习的朋友可参考参考.ftp_get() 函数从 FTP 服务器上下载一个文件.若成功则返回 true,失败则返回 false. ...

  2. xtrabackup工具

    xtrabackup是基于InnoDB存储引擎灾难恢复的.它复制InnoDB的数据文件,尽管数据文件在内部是非一致性的,但在执行灾难恢复时可以保证这些数据文件是一致的,并且可用. 官方原理 在Inno ...

  3. Acunetix Web漏洞扫描器

    1.主要程序介绍 主要操作区域简介: b).工具栏 从左到右分别是(这些都可以在主要操作区域找到,所以不常用): 新建扫描——网站扫描——网站爬行——目标查找——目标探测——子域名扫描——SQL盲注— ...

  4. Sudoku Solver Backtracking

    该博客好好分析 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicat ...

  5. PYTHON第二天

    PYTHON之路 4.注释 Python开发规范:每一行不能超过80个字符 # : 代表注释当行 ‘’’  上下两行  ,  三个英文字符的引号,代表注释某一段代码 ‘’’ 知识点用户输入: PYth ...

  6. js 操作map对象

    转自:http://smallvq123.javaeye.com/blog/823923 /* * Map对象,实现Map功能 * * * size() 获取Map元素个数 * isEmpty() 判 ...

  7. ComponentOne Xuni助力Xamarin开发者突破百万,快速开发Android、IOS Apps

    在微软Build 2015上,随着VS 2015的预览版发布,Xamrine免费版已经作为VS 2015跨平台移动解决方案的核心.与此同时,Xamarin官方也宣布其用户量达到百万之多.2011年7月 ...

  8. dpkg 被中断,您必须手工运行 sudo dpkg -configure -a 解决

    E: dpkg 被中断,您必须手工运行 sudo dpkg --configure -a 解决此问题. E: dpkg 被中断,您必须手工运行 sudo dpkg --configure -a 解决此 ...

  9. javascript变量名提升

    预解析的过程 代码的执行过程 程序在执行过程,会先将代码读取到内存中检查,会将所有的声明在此时进行标记.所谓的标记就是让js解释器直到有这个名字,后面在使用名字的时候,不会出现未定义的错误,这个标记就 ...

  10. 易出错的C语言题目之二:指针

    一.写出输出结果 #include<stdio.h> int main(){ ]; a[] = ; a[] = ; a[] = ; int *p,*q; p = a; q = &a ...