[LeetCode] Range Sum Query 2D - Immutable
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的更多相关文章
- [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 ...
- [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 ...
- 【刷题-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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- 【LeetCode】304. Range Sum Query 2D - Immutable 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 预先求和 相似题目 参考资料 日期 题目地址:htt ...
- LeetCode Range Sum Query 2D - Mutable
原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...
随机推荐
- Nginx学习笔记(四) 源码分析&socket/UDP/shmem
源码分析 在茫茫的源码中,看到了几个好像挺熟悉的名字(socket/UDP/shmem).那就来看看这个文件吧!从简单的开始~~~ src/os/unix/Ngx_socket.h&Ngx_s ...
- [MFC] 从文件读取与向文件添加数据
CString str,str2,str3;str2="dsf",str3="dsfds"; CStdioFile myFile, File; if(myFil ...
- [WinAPI] API 8 [获取磁盘空间信息]
获取磁盘分区的总容量和空闲空间的容量可以使用GetDiskFreeSpace函数或GetDiskFree SpaceEx函数.GetDiskFreeSpace使用DWORD类型作为输出参数,由于DWO ...
- [51单片机] HC-SR04超声波测距仪 基础代码
>_<:超声波测距仪模块: >_<:51单片机,11.0592MHz晶振,将采集数据发送到串口的基础例子: >_<:代码: /******************* ...
- EF Code First Migration总结
开启Migration 1. 通过 Tools->Nuget Package Manager->Package Manager Console 打开Package Manager Cons ...
- iOS YSAuth 第三方登录及分享
概述 在很多时候,我们使用的都是第三方插件来处理登录与分享,比如使用友盟.但要是我们需要自己写一套自己的登录.分享.支付的时候,就可以使用YSAuth这个类库. github:https://gith ...
- 基于.NET的Excel开发:单元格区域的操作(读取、赋值、边框和格式)
引用 using Excel = Microsoft.Office.Interop.Excel; 定义 Excel.ApplicationClass app; Excel.Workbooks book ...
- python decorator simple example
Why we need the decorator in python ? Let's see a example: #!/usr/bin/python def fun_1(x): return x* ...
- atitit.团队建设总结fx O622
团队建设总结fx O622 大的title 2 建设团队文化 2 办公环境(3s+树+湖) 3 每人一个办公室 3 弹性工作制 3 重大的决定公投体制 3 每年一个kid经验日 3 做自己想到做的事. ...
- Django网站管理、后台、模型(Admin)
1. 启动wampserver 2. 新建一个名为mysite的项目django-admin.py startproject mysite 3. 新建一个叫做blog的appcd mysitepyth ...