[LeetCode] Range Sum Query 2D - Immutable
2015-11-12 22:00
330 查看
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.
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 = 0; i < matrix.size(); i++) for (int j = 0; j < matrix[0].size(); j++) accu[i][j] += a(i-1, j) + a(i, j-1) - a(i-1, j-1); } int sumRegion(int row1, int col1, int row2, int col2) { return a(row2, col2) - a(row1-1, col2) - a(row2, col1-1) + a(row1-1, col1-1); } private: vector<vector<int>> accu; int a(int i, int j) { return i >= 0 && j >= 0 ? accu[i][j] : 0; } }; // 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);
相关文章推荐
- ptlib编译时,会自动扫描电脑系统,找出头文件以及库文件并进行引用,生成头文件ptbuildopts.h
- Caffe编译出问题:This tool requires OpenCV; compile with USE_OPENCV.
- IQueryable join 的问题
- codeforces-441B-Valera and Fruits【暴力】
- 606第九周周六赛 D - Buildings
- SQLiteDatabase中query、insert、update、delete方法参数说明
- UI第三天:⾃定义视图、视图控制器
- 关于Activity销毁,而绘制UI的子线程未销毁出现的问题
- How to queue a heartbeat to get faster when an enemy is near
- Unable to create a constant value of type 'Closure type'
- POJ 2031 Building a Space Station【最小生成树 Kruskal】
- Extending query processing with search components
- UIImageView 圆形
- Codeforces Testing Round #12 C. Subsequences 树状数组维护DP
- UILabel UISwitch UISegmentedControl UIAlertView
- valueOf跟toString区别
- 《duilib入门到精通》- duilib概述
- NRV (named return value)
- UISwitch开关
- 《iOS Human Interface Guidelines》——Color and Typography