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. v-for遍历出的元素上添加click事件,获取对应元素上的属性id值

    <span v-for="(n,nav) in floorList" data-id="{{nav.itemId}}" v-on:click=" ...

  2. JVM:查看java内存情况命令

    jmap (linux下特有,也是很常用的一个命令) 观察运行中的jvm物理内存的占用情况. 参数如下: -heap :打印jvm heap的情况 -histo: 打印jvm heap的直方图.其输出 ...

  3. JVM内存垃圾回收方法

    1.概述 1.1.为什么要回收? 如果不进行垃圾回收,内存迟早都会被消耗空,因为我们在不断地分配内存空间而不进行回收.除非内存无限大,我们可以任性地分配而不回收,但是事实并非如此.所以,垃圾回收是必须 ...

  4. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' heade

    XMLHttpRequest cannot load http://10.164.153.37:8050/WebService/WebService.asmx/wsGetStreetData. Res ...

  5. CH模拟赛 还教室

    /* 区间操作,可以推一推式子,方差为平方的平均数-平均数的平方,维护区间和与区间平方和,平方和的维护方法类似,式子推一推就行了,注意约分 */ #include<iostream> #i ...

  6. eclipse新建web项目开发JSP

    1.创建项目:file---new--Dynamic Web Project 一直选next,到jsp文件目录所在地,打勾默认自动生成web.xml配置文件,也可以自己设置. 创建JSP文件: 选择创 ...

  7. mybatis逆向工程生成JavaBean、dao、mapper generatorSqlmapCustom

    import java.io.File; import java.util.ArrayList; import java.util.List; import org.mybatis.generator ...

  8. iOS sqlite数据库图像化查看

    问题描述:在xocde上用sqlite数据库的时候,因为没有图形化界面,有些时候很难看出自己设计的数据库是否有问题,比如我刚上手sqlite数据库设计id为自增长时,很自然的用了identify(1, ...

  9. 利用SQLite_Expert实现Excel表转SqLite数据库

    1.保留excel数据中需要的字段,删除无关字段. 2.将excel另存为cvs格式文件.<另存名称如:jizhan.cvs 则导入后表名即为jizhan> 3.看下图,新建数据库,命名为 ...

  10. 使用ROW_NUMBER()+临时表+While 实现表遍历

    declare @table table(dlid int,RowNum int)insert into @table select dlid,ROW_NUMBER() over(order by d ...