Java实现 LeetCode 304 二维区域和检索 - 矩阵不可变
304. 二维区域和检索 - 矩阵不可变
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。
Range Sum Query 2D
上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8。
示例:
给定 matrix = [
[3, 0, 1, 4, 2],
[5, 6, 3, 2, 1],
[1, 2, 0, 1, 5],
[4, 1, 0, 1, 7],
[1, 0, 3, 0, 5]
]
sumRegion(2, 1, 4, 3) -> 8
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12
说明:
你可以假设矩阵不可变。
会多次调用 sumRegion 方法。
你可以假设 row1 ≤ row2 且 col1 ≤ col2。
class NumMatrix {
static int[][] data = new int[1050][1050];
public NumMatrix(int[][] matrix) {
if(matrix.length > 0){
for(int i = 0;i < matrix.length;i++){
int sum = 0;
for(int j = 1;j <= matrix[0].length;j++){
sum += matrix[i][j - 1];
data[i][j] = sum;
}
}
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
int sum = 0;
for(int i = row1;i <= row2;i++ ){
sum += data[i][col2 + 1] - data[i][col1];
}
return sum;
}
}
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/
Java实现 LeetCode 304 二维区域和检索 - 矩阵不可变的更多相关文章
- Leetcode 304.二维区域和检索-矩阵不可变
二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, c ...
- LeetCode 304. Range Sum Query 2D - Immutable 二维区域和检索 - 矩阵不可变(C++/Java)
题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- [Swift]LeetCode304. 二维区域和检索 - 矩阵不可变 | 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]303.区域和检索&&304.二维区域和检索
题目 1.区域和检索: 简单题,前缀和方法 乍一看就觉得应该用前缀和来做,一个数组多次查询. 实现方法: 新建一个private数组prefix_sum[i],用来存储nums前i个数组的和, 需要找 ...
- 领扣(LeetCode)二维区域和检索 个人题解
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...
- [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 ...
- [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 lef ...
- 304 Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...
随机推荐
- Arrays.binarySearch的返回值
如果查找的值包含在数组中,返回搜索的第一个值的下标: 如果查找的值不在数组中,返回(-插入点-1):插入点即为第一个大于此查找值的元素下标 插入点 为将该值插入数组的那一点:即第一个大于此键的元素下标 ...
- Java SPI机制简述
概述 SPI全称Service Provider Interface,是一种为框架提供良好扩展性的机制.一般由框架开发方定义接口规范(如java.sql.Driver),而第三方厂商为之提供自己的实现 ...
- SQLServer分组加序号,只取某个对象指定条件的前几个
-- -- 删除base里冗余的数据 --UPDATE dbo.N_Order_ServiceLog SET IsDel = 1 WHERE OrderId IN ( SELECT OrderI ...
- Vue+Vuex 实现自动登录功能
刚刚实现了Vue+Vuex的自动登录功能,在实现的时候遇到了一些问题,这里记录一下: 因为这个还不够完善,在写完下列代码后,又进行了补充,可以从https://www.cnblogs.com/xiao ...
- css多行省略和单行省略
实现文本省略: <!-- html代码 --> <p class="single">该文的主题思想即对自由境界的向往.朱自清当时虽置身在污浊黑暗的旧中国,但 ...
- React:Component
web开发由web pages过渡到web app 后,开发的模式也发生了变化,由传统的主张结构.样式.行为分离到现在的组件化,把应用的各个部分看成解耦的部分,每部分自包含js.css和html,以方 ...
- (数据科学学习手札84)基于geopandas的空间数据分析——空间计算篇(上)
本文示例代码.数据及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 在本系列之前的文章中我们主要讨论了g ...
- Excel日期转换为PHP时间戳
PHP 的时间函数是从1970-1-1日开始计算的,单位是秒数.但是 EXCEL的是从1900-1-1日开始算的单位是天数. 如果只计算1970以后的时间的话,就好处理了. 先获得 EXCEL中 19 ...
- Java-建立一个AnalysisResult类,对成绩进行分析
import java.io.*; import java.util.*; public class AnalysisResult { public static void main(String[] ...
- (三)vue数据绑定及相应的命令
vue数据绑定及相应的命令 {{ Text }} 双括号进行数据渲染 动态绑定数据 例如:{{message}} data: { return{ message: 'Hello Vue!' } } 2 ...