Range Sum Query 2D - Immutable
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的更多相关文章
- [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 - 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
题目 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- [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 ...
- [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 ...
- 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
Description: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by ...
随机推荐
- IntelliJ IDEA mac 快捷键
cmd+O 查找类alt+cmd+O 符号shift+cmd+O 查找文件^+space 补全alt + F7 查找符号的使用情况F1 查看文档cmd+b 跳转定义,或者 鼠标+ctrlcmd+F12 ...
- TeamViewer 12.0.71503 Patch By.Sound
TeamViewer - the All-In-One Software for Remote Support and Online Meetings - Remote control any com ...
- 海拔高度图*.dem文件的读取—vtkDEMReader
vtkDEMReader reads digital elevation files and creates image data. Digital elevation files are produ ...
- iOS多线程-GCD之常用函数
延迟执行任务函数dispatch_after(.....) -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEve ...
- HTML插入FLASH
<object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" code ...
- webservice客户端添加soap Header信息
根据wsdl文件的header信息,在客户端中添加相应的header 1.wsdl信息如图 <soapenv:Envelope xmlns:soapenv="http://schema ...
- 小白 安装和配置Tomcat 局域网内访问网页
1.官网http://tomcat.apache.org/ ,下载tomcat,解压就好 2.官网www.oracle.com, 下载javaJDK,截图如下,点击黄色荧光笔
- MySQL复制和集群
一.复制配置 (A) 主从服务器相同版本的数据库 (B) 主服务器上复制使用的账户,具有相应的权限. (C) 修改主服务器的配置文件my.cnf,开启BINLOG,并设置server-id的值.重启后 ...
- Android的Intent.FLAG_ACTIVITY_CLEAR_TOP无效
转载:http://blog.csdn.net/u011361576/article/details/48626237 今天写代码遇到了一个问题: 当 B - A - B 跳转的时候,使用Intent ...
- Android学习资料收集
1.Android 学习之路 http://stormzhang.com/android/2014/07/07/learn-android-from-rookie/