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.

题解:

二维的情况和一维的思路类似,就是求一个递推式然后把和预处理出来,然后O(1)查询。

一维的递推式是:s[i]=s[i-1]+a[i];

二维的由画图可知:s[i][j]=a[i][j]+s[i-1][j]+s[i][j-1]-s[i-1][j-1];

class NumMatrix {
public:
NumMatrix(vector<vector<int>> &matrix) {
n=matrix.size();
m=n>?matrix[].size():;
s=vector<vector<int>>(n+,vector<int>(m+,));
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
s[i][j]=matrix[i-][j-]+s[i-][j]+s[i][j-]-s[i-][j-];
}
}
} int sumRegion(int row1, int col1, int row2, int col2) {
return s[row2+][col2+]-s[row2+][col1]-s[row1][col2+]+s[row1][col1];
}
private:
int n,m;
vector<vector<int> >s;
}; // 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 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 二维区域和检索 - 矩阵不可变(C++/Java)

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

  5. 【刷题-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 ...

  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. C语言数据类型的转换

    C语言的类型转换,一个是强制类型进行转换,而在这里要介绍的是自动的数据类型的转换,自动的数据类型转换很多时候是发生在多种数据类型混合使用的时候就会进行类型的转换,这样就会带来不能控制的结果,所以必须进 ...

  2. 1BIT,1BYTE,1KB,1MB,1GB,1TB等计量单位换算

    http://iask.sina.com.cn/b/8961090.html知识   在数字世界里没有电影.没有杂志.没有一首首的乐曲,只有一个个的数字“1”和“0”.以前人们对于数字世界中的这两个数 ...

  3. webdriver.py--解说

    一.全局操作类 start_session 使用指定的desired capabilities创建一个会话(session)start_client 新建一个webdriver会话session前调用 ...

  4. Python 规范化LinkedIn用户联系人的职位名

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-19 @author: guaguastd @name: j ...

  5. 笔记本中G-Sensor(加速计) M-Sensor 陀螺仪等传感器的区别

    1.G-sensor重力传感器 作用 G-sensor中文是加速度传感器的意思(英文全称是Accelerometer-sensor),它能够感知到加速力的变化,加速力就是当物体在加速过程中作用在物体上 ...

  6. 在windows下安装apidocjs

    1. 下载Node.js官方Windows版程序:   https://nodejs.org/download/   从0.6.1开始,Node.js在Windows平台上提供了两种安装方式,一是.M ...

  7. Spring Boot从入门到实战:整合Web项目常用功能

    在Web应用开发过程中,一般都涵盖一些常用功能的实现,如数据库访问.异常处理.消息队列.缓存服务.OSS服务,以及接口日志配置,接口文档生成等.如果每个项目都来一套,则既费力又难以维护.可以通过Spr ...

  8. Bootstrap学习速查表(三) 表单

    表单中常见的元素主要包括:文本输入框.下拉选择框.单选按钮.复选按钮.文本域和按钮等. 一.基础表单 1.初始化:对于基础表单,Bootstrap并未对其做太多的定制性效果设计,仅仅对表单内的fiel ...

  9. substring,subsequence,charAt执行效率的不同

    package com.java.tencent; public class T_2_longestPalindrome { public String test1(String s){ long s ...

  10. hdu4612 无向图中随意加入一条边后使桥的数量最少 / 无向图缩点+求树的直径

    题意如上,含有重边(重边的话,俩个点就能够构成了边双连通). 先缩点成树,在求数的直径,最远的连起来,剩下边(桥)的自然最少.这里学习了树的直径求法:第一次选随意起点U,进行bfs,到达最远的一个点v ...