Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fact, if you work in computer vision, you may know a name for such an array --- Integral Image.

To solve this problem, Stefan has already posted a very elegant solution.

The code is copied here.

 class NumMatrix {
public:
NumMatrix(vector<vector<int>> &matrix) {
accu = matrix;
for (int i = ; i < matrix.size(); i++)
for (int j = ; j < matrix[].size(); j++)
accu[i][j] += a(i-, j) + a(i, j-) - a(i-, j-);
} int sumRegion(int row1, int col1, int row2, int col2) {
return a(row2, col2) - a(row1-, col2) - a(row2, col1-) + a(row1-, col1-);
}
private:
vector<vector<int>> accu;
int a(int i, int j) {
return i >= && j >= ? accu[i][j] : ;
}
}; // 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 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] Range Sum Query - Immutable & Range Sum Query 2D - Immutable

    Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indice ...

  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] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

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

  5. Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree

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

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

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

  8. 【LeetCode】304. Range Sum Query 2D - Immutable 解题报告(Python)

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

  9. LeetCode Range Sum Query 2D - Mutable

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

随机推荐

  1. Kali Linux Web 渗透测试视频教—第二十课-利用kali linux光盘或者usb启动盘破解windows密码

    Kali Linux Web 渗透测试视频教—第二十课-利用kali linux光盘或者usb启动盘破解windows密码 文/玄魂 目录 Kali Linux Web 渗透测试视频教—第二十课-利用 ...

  2. Ubuntu 安装配置MySQL,并使用VS的Server Explorer UI界面远程管理MySQL

    为安装配置方便,使用root账号登入Ubuntu. step1: 键入下面命令安装MySQL. 过程十分简单.安装过程中只需根据提示输入root账号的密码即可. step2:安装完成后检查MySQL是 ...

  3. iOS YSMine 通用设置

    概述 我们在开发的过程中,经常需要重复的写个人中心设置的代码,很多情况下,我们都是通过if(indexPath.row)来判断操作以及要跳转的页面,这样的情况是非常不友好的,尤其是当我们需要调整显示顺 ...

  4. iOS YSKit系列

    YSKit是基于object-c的一套常用功能点的总结,都是我们在平时开发的过程中遇到的.这个框架将会慢慢地完善,由于本人精力有限,有时可能更新得相对慢一点.如果发现代码里有错误,也欢迎提出. 1.常 ...

  5. 【Android】AppCompat V21:将 Materia Design 兼容到5.0之前的设备

    AppCompat V21:将 Materia Design 兼容到于5.0之前的设备 本篇文章翻译自Chris Banes(就职于Google,是Android-PullToRefresh,Phot ...

  6. Atitit.如何选择技术职业方向

    Atitit.如何选择技术职业方向 1. 原则是应该如下的应该从以下指标判断1 1.1. 技术的长寿性(长生命周期1 1.2. 技术的普适性(市场份额)1 1.3. **属于open体系还是封闭体系? ...

  7. java集合练习——题目

    2.已知有十六支男子足球队参加2008 北京奥运会.写一个程序,把这16 支球队随机分为4 个组.采用List集合和随机数 2008 北京奥运会男足参赛国家: 科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷 ...

  8. RTMP协议

    Real Time Messaging Protocol(实时消息传送协议协议)概述   实时消息传送协议是Adobe Systems公司为Flash播放器和服务器之间音频.视频和数据传输开发的私有协 ...

  9. FIREDAC操作SQLITE内存数据库

    SQLite不仅可以把数据库放在硬盘上,还可以放在内存中,经测试,同样条件下数据库放在内存中比放在硬盘上插入记录速度快差不多3倍. 但数据库放在内存中时有如下缺陷: 1.断电或程序崩溃后数据库就会消失 ...

  10. Linux下远程桌面Windows

    rdesktop-1.7.0.tar.gz [root@localhost fcitx]# tar rdesktop-1.7.0.tar.gz [root@localhost fcitx]#cd rd ...