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.

  1. class NumMatrix {
  2. public:
  3. NumMatrix(vector<vector<int>> &matrix) {
  4. accu = matrix;
  5. for (int i = ; i < matrix.size(); i++)
  6. for (int j = ; j < matrix[].size(); j++)
  7. accu[i][j] += a(i-, j) + a(i, j-) - a(i-, j-);
  8. }
  9.  
  10. int sumRegion(int row1, int col1, int row2, int col2) {
  11. return a(row2, col2) - a(row1-, col2) - a(row2, col1-) + a(row1-, col1-);
  12. }
  13. private:
  14. vector<vector<int>> accu;
  15. int a(int i, int j) {
  16. return i >= && j >= ? accu[i][j] : ;
  17. }
  18. };
  19.  
  20. // Your NumMatrix object will be instantiated and called as such:
  21. // NumMatrix numMatrix(matrix);
  22. // numMatrix.sumRegion(0, 1, 2, 3);
  23. // 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. 客户端缓存 HTML + 远程数据 JS 的思路。

    移动客户端,采用客户端集成 WebBrowser 的方式 ,加载远程网页的优化方案. 1. 远程 HTML版本 v1.2 一次性加载到客户端 2. 手机端打开时,检测HTML版本. 如果有新版,先更新 ...

  2. sqlserver 链接 ODBC 访问 MySql

    环境:windows 2008 + sqlserver 2008 一 安装 mysql-connector-odbc-5.2.5-winx64.msi 必须安装5.2.5,安装mysql-connec ...

  3. [外挂8] 自动挂机 SetTimer函数

    >_< : 这里用SetTimer函数自动运行![注意添加在里面的回掉函数函数] UINT SetTimer( HWND hWnd, // 指向窗口句柄 UINT nIDEvent, // ...

  4. atitit.提升开发效率---MDA 软件开发方式的革命(3)----自动化建表

    atitit.提升开发效率---MDA 软件开发方式的革命(3)----自动化建表 1. 建模在后自动建表 1 1. 传统上,需要首先建表,在业务编码.. 1 2. 模型驱动建表---更多简化法是在建 ...

  5. paip.批处理清理java项目冗余jar的方法

    paip.批处理清理java项目冗余jar的方法 在myeclipse中开发的java项目遇到jar包冗余情况,如何删除项目中的冗余jar包啊?项目很大jar包一百多个. 2010-09-14 14: ...

  6. D. Book of Evil

    D. Book of Evil time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  7. XML入门级的简单学习

    xml案例<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Ge ...

  8. Spring简介和基础

    Spring介绍 1.什么事Spring? spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架. spring的设计模式是单例模式和工厂模式. 2.spring的四大优点 轻量 ...

  9. php-fpm的配置和优化

    php-fpm的安装目录 下面是我的平时的环境搭建php的各种安装目录,大家的基本也差不多. centos等linux平台 /usr/local/php/php /usr/local/php/etc/ ...

  10. Android ImageView圆形头像

    转载自:http://m.oschina.net/blog/321024 Android ImageView圆形头像 图片完全解析 我们在做项目的时候会用到圆形的图片,比如用户头像,类似QQ.用户在用 ...