https://leetcode.com/problems/range-sum-query-2d-immutable/

条件说sumRegion 会调很多次,如果每次都用双for 循环去累加的话就有太多重复计算了,所以可以实现cache 一下。

可以有两种cache 机制,一种是在初始化的时候就生成cache 这样初始化时间比较长应该是O(n*m) 但是sumRegion 方法就方便和高效多了,大概是O(1)

另一种是动态更新cache,随着sumRegion 的调用慢慢建立cache,这样处理起来会比较麻烦,但是初始化时间就可以忽略不计了,在sumRegion 的多次调用中慢慢的速度越来越快。

我才用第一种方式,初始化时候建立cache,cache 同样是n*m 的矩阵,每个元素是当前行的累加和。

调用sumRegion 的时候把每行的指定列范围内的和加起来就行了,而每行的和就用两个边际列的cache 值只差加上左边际列的原始值就行了,即:

rowSum = cache[endCol] - cache[starCol] + origin[starCol]

/**
* @constructor
* @param {number[][]} matrix
*/
var NumMatrix = function(matrix) {
if (matrix.length === 0) return;
this.cache = [];
this.matrix = matrix;
this.row = matrix.length;
this.col = matrix[0].length; for (var i = 0; i < this.row; i++) {
var acc = 0;
this.cache.push([]);
for (var j = 0; j < this.col; j++) {
acc += matrix[i][j];
this.cache[i].push(acc);
}
}
// console.log(this.cache);
}; /**
* @param {number} row1
* @param {number} col1
* @param {number} row2
* @param {number} col2
* @return {number}
*/
NumMatrix.prototype.sumRegion = function(row1, col1, row2, col2) {
var sum = 0;
if (!this.matrix) return 0;
for (var i = row1; i <= row2; i++) {
sum += this.cache[i][col2] - this.cache[i][col1] + this.matrix[i][col1];
}
return sum;
};

Range Sum Query 2D - Immutable的更多相关文章

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

  2. 【刷题-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 ...

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

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

  5. [LeetCode] Range Sum Query 2D - Immutable

    Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fac ...

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

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

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

  8. 304. Range Sum Query 2D - Immutable

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

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

  10. LeetCode-304. Range Sum Query 2D - Immutable

    Description: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by ...

随机推荐

  1. MSSQLServer中组织或分类表的设计及其递归查询

    开篇:项目中用到上下级从属关系的太多太多了,如:组织.分类.行政区域,这里不再一一介绍,遇到这种的如何去进行数据库表的设计及其应用的,个人对往期项目中所涉及到的进行了一些总结. 数据库表设计:表字段一 ...

  2. apache_commons 之 双向Map DualHashBidiMap (使用及源码)

    在项目当中,经常出现需要根据Key值获取value:而且要求根据value获取key值,其实在commons-collections包中已经提供了此集合类.就是DualHashBidiMap类. (官 ...

  3. redis-window 集群配置

    参考文章: 1.http://www.cnblogs.com/zr520/p/5057141.html (主从配置) 2.http://www.cnblogs.com/lori/p/5825691.h ...

  4. python 类定义 继承

    0 前言 系统:win7 64bit IDE : python(x,y) 2.7.6.1 IDE集成的解释器:Python 2.7.6 (default, Nov 10 2013, 19:24:18) ...

  5. 看着水了一天的群,终于看到一段高质量的代码了分享一下localStorage

    _history : { //缓存 isLocalStorage:window.localStorage?true:false, set : function(key,value){ //设置缓存 i ...

  6. vtkQuadratic创建半球面

    用的关键类:vtkQuadric.vtkSampleFunction.vtkContourFilter:用于创建方框的类vtkOutlineFilter #ifndef INITIAL_OPENGL ...

  7. laravel数据库迁移(三)

    laravel号称世界上最好的框架,数据库迁移算上一个,在这里先简单入个门: laravel很强大,它把表中的操作写成了migrations迁移文件,然后可以直接通过迁移文件来操作表.所以 , 数据迁 ...

  8. Python中函数、类、模块和包的调用

    初学python阶段,大多数人对函数.类.模块和包的调用都搞得不是很清楚,这篇随笔就简单的进行说明. (1)函数 当函数定义好之后,可以直接调用. 比如:def summ(add1,add2),那么 ...

  9. linux 下安装 mysql5.7.16安装

    1.groupadd mysql      ## 添加一个mysql组 2.useradd -r -g mysql mysql    ## 添加一个用户 3.解压缩下载的包,tar -xzvf /da ...

  10. JS 深浅拷贝

    首先理解概念 浅拷贝: 只复制对象的基本类型, 对象类型, 仍属于原来的引用. 深拷贝: 不紧复制对象的基本类, 同时也复制原对象中的对象.就是说完全是新对象产生的. 首先看浅拷贝 //浅拷贝 var ...