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. Django 部署(Apache下)

    前言: 因为需要在服务器下运行python脚本,所以需要搭建Django服务器.所以将自己的学习过程也记录下来,方便日后查阅. 本文环境如下: Ubuntu 16.04  python2.7 Apac ...

  2. SpringMvc接受特殊符号参数被转义

    WEB开发时,在前端通过get / post 方法传递参数的时候  如果实参附带特殊符号,后端接收到的值中特殊符号就会被转义 例如该请求: http://localhost:10001/demo/in ...

  3. 如何用纯 CSS 创作一个摇摇晃晃的 loader

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览.https://codepen.io/comehope/pen/oyJvpe 可交互视频 此 ...

  4. 20145216史婧瑶《Java程序设计》第5周学习总结

    20145216 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 异常处理 8.1 语法与继承架构 Java中所有错误都会被打包为对象,运用try.catch,可以在错误发生 ...

  5. 都能看懂的嵌入式linux/android alsa_aplay alsa_amixer命令行用法

    前几天在嵌入式linux上用到alsa command,网上查的资料多不给力,只有动手一点点查,终于可以用了,将这个使用方法告诉大家,以免大家少走弯路. 0.先查看系统支持哪几个alsa cmd: l ...

  6. Win7下硬盘安装fedora17

    Win7下硬盘安装fedora17 这几天经过很多次的百度和实验,终于成功的在我的x64机子上装上了fedora17,以此分享给大家,希望能给大家帮助. 一.准备工作: 1.工具,因为我们电脑上大部分 ...

  7. mfc配置CAN通信

    配置:把kerneldlls文件夹.ControlCAN.dll.ControlCAN.lib放在工程下面(debug和Release下面,最后需要exe和这些文件在一起):右键工程属性,链接器-&g ...

  8. 项目总结--基于Cortex-A9平台的米兰花智能培育系统

    基于Cortex-A9平台的米兰花智能培育系统 1. 系统功能概述 本系统主要实现了模拟米兰花智能培育的过程.通过前端传感器采集相关环境因子数据经ZigBee组网发送到协调器汇总,网关通过串口读取协调 ...

  9. 爬虫之动态HTML处理(Selenium与PhantomJS )执行 JavaScript 语句

    执行 JavaScript 语句 1.隐藏百度图片 from selenium import webdriverimport time driver = webdriver.PhantomJS()dr ...

  10. 【Network architecture】Rethinking the Inception Architecture for Computer Vision(inception-v3)论文解析

    目录 0. paper link 1. Overview 2. Four General Design Principles 3. Factorizing Convolutions with Larg ...