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. linux显示中文

    设置centos显示中文   怎么设置Linux系统中文语言,这是很多小伙伴在开始使用Linux的时候,都会遇到一个问题,就是终端输入命令回显的时候中文显示乱码.出现这个情况一般是由于没有安装中文语言 ...

  2. ig WebDataGrid清除选中行

    EnableViewState="false" gridID.Behaviors.Selection.SelectedRows.Clear();

  3. java 反射

    com.my.Ob; @Table(name="ob") class Ob{ @Id private Integer id; @Column(name="name1&qu ...

  4. 机器学习——k-近邻算法

    k-近邻算法(kNN)采用测量不同特征值之间的距离方法进行分类. 优点:精度高.对异常值不敏感.无数据输入假定 缺点:计算复杂度高.空间复杂度高 使用数据范围:数值型和标称型 工作原理:存在一个样本数 ...

  5. Spring特性--DI

    DI:Dependency Injection(依赖注入),通俗的讲就是一种通过xml配置文件,为交给sping容器的对象初始化参数.又称做控制反转:Inversion of Control(IoC) ...

  6. 如何在Texstudio内加载语法检查词典?

    如何在Texstudio编辑软件内加载"语法检查词典"? How to make dictionary work in TexStudio I am using TexStudio ...

  7. JavaScript - 正则表达之二

    正则表达式的大致匹配过程是:依次拿出表达式和文本中的字符比较,如果每一个字符都能匹配,则匹配成功:一旦有匹配不成功的字符则匹配失败. 正则表达式通常用于在文本中查找匹配的字符串.Python里数量词默 ...

  8. php curl 多线程方法

    <?php /** * curl 多线程 * @param array $array 并行网址 * @param int $timeout 超时时间 * @return array */ fun ...

  9. react+redux官方实例TODO从最简单的入门(4)-- 改

    上一篇文章实现了<删>这个功能,那么我们继续添加下一功能--<改> 这个修改的功能是通过双击每个子选项实现 第一步:按规矩来,添加一个状态声明 第二步:action中约定我们要 ...

  10. win10进入安全模式的方法(F8不管用/开不开机)

    win10默认不能进入安全模式,这时候开机黑屏怎么办?下面介绍强制进入安全模式的方法 1. 关机情况下,按开机键开机,等到出现徽标(下图),这时候长按开机键强制关机. 2. 再次开机,出现徽标,再次强 ...