[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 ...
随机推荐
- 客户端缓存 HTML + 远程数据 JS 的思路。
移动客户端,采用客户端集成 WebBrowser 的方式 ,加载远程网页的优化方案. 1. 远程 HTML版本 v1.2 一次性加载到客户端 2. 手机端打开时,检测HTML版本. 如果有新版,先更新 ...
- sqlserver 链接 ODBC 访问 MySql
环境:windows 2008 + sqlserver 2008 一 安装 mysql-connector-odbc-5.2.5-winx64.msi 必须安装5.2.5,安装mysql-connec ...
- [外挂8] 自动挂机 SetTimer函数
>_< : 这里用SetTimer函数自动运行![注意添加在里面的回掉函数函数] UINT SetTimer( HWND hWnd, // 指向窗口句柄 UINT nIDEvent, // ...
- atitit.提升开发效率---MDA 软件开发方式的革命(3)----自动化建表
atitit.提升开发效率---MDA 软件开发方式的革命(3)----自动化建表 1. 建模在后自动建表 1 1. 传统上,需要首先建表,在业务编码.. 1 2. 模型驱动建表---更多简化法是在建 ...
- paip.批处理清理java项目冗余jar的方法
paip.批处理清理java项目冗余jar的方法 在myeclipse中开发的java项目遇到jar包冗余情况,如何删除项目中的冗余jar包啊?项目很大jar包一百多个. 2010-09-14 14: ...
- D. Book of Evil
D. Book of Evil time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- XML入门级的简单学习
xml案例<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Ge ...
- Spring简介和基础
Spring介绍 1.什么事Spring? spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架. spring的设计模式是单例模式和工厂模式. 2.spring的四大优点 轻量 ...
- php-fpm的配置和优化
php-fpm的安装目录 下面是我的平时的环境搭建php的各种安装目录,大家的基本也差不多. centos等linux平台 /usr/local/php/php /usr/local/php/etc/ ...
- Android ImageView圆形头像
转载自:http://m.oschina.net/blog/321024 Android ImageView圆形头像 图片完全解析 我们在做项目的时候会用到圆形的图片,比如用户头像,类似QQ.用户在用 ...