当前位置:网站首页>剑指 Offer II 013. 二维子矩阵的和
剑指 Offer II 013. 二维子矩阵的和
2022-07-07 17:37:00 【瑾怀轩】
给定一个二维矩阵 matrix,以下类型的多个请求:
计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2) 。
实现 NumMatrix 类:
NumMatrix(int[][] matrix) 给定整数矩阵 matrix 进行初始化
int sumRegion(int row1, int col1, int row2, int col2) 返回左上角 (row1, col1) 、右下角 (row2, col2) 的子矩阵的元素总和。
示例 1:

输入:
["NumMatrix","sumRegion","sumRegion","sumRegion"]
[[[[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]]],[2,1,4,3],[1,1,2,2],[1,2,2,4]]
输出:
[null, 8, 11, 12]
解释:
NumMatrix numMatrix = new NumMatrix([[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]]]);
numMatrix.sumRegion(2, 1, 4, 3); // return 8 (红色矩形框的元素总和)
numMatrix.sumRegion(1, 1, 2, 2); // return 11 (绿色矩形框的元素总和)
numMatrix.sumRegion(1, 2, 2, 4); // return 12 (蓝色矩形框的元素总和)
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/O4NDxx
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
java:
class NumMatrix {
public int[][] mat;
public NumMatrix(int[][] 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]]]
this.mat = matrix;
}
public int sumRegion(int row1, int col1, int row2, int col2) {
int total = 0;
//求取给定下标的值
for(int i = row1;i <= row2 ; i++){
for(int j = col1;j<=col2;j++){
total += this.mat[i][j];
}
}
return total;
}
}边栏推荐
- L1-025 positive integer a+b (Lua)
- what‘s the meaning of inference
- R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the dot strip plot, set the position parameter, and configure the separation degree of different grouped
- MySQL、sqlserver oracle数据库连接方式
- 位运算介绍
- R language dplyr package select function, group_ The by function, filter function and do function obtain the third largest value of a specific numerical data column in a specified level in a specified
- 吞吐量Throughout
- The DBSCAN function of FPC package of R language performs density clustering analysis on data, checks the clustering labels of all samples, and the table function calculates the two-dimensional contin
- # 欢迎使用Markdown编辑器
- 实训九 网络服务的基本配置
猜你喜欢

9 atomic operation class 18 Rohan enhancement

一张图深入的理解FP/FN/Precision/Recall
Make this crmeb single merchant wechat mall system popular, so easy to use!

杰理之关于 TWS 配对方式配置【篇】

Make insurance more "safe"! Kirin Xin'an one cloud multi-core cloud desktop won the bid of China Life Insurance, helping the innovation and development of financial and insurance information technolog

杰理之测试盒配置声道【篇】

Former richest man, addicted to farming

PV static creation and dynamic creation

Command mode - unity

杰理之发起对耳配对、回连、开启可发现、可连接的轮循函数【篇】
随机推荐
Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica
Browse the purpose of point setting
Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
Numpy——2.数组的形状
2022.07.04
位运算介绍
ant desgin 多选
R language dplyr package mutate_ At function and min_ The rank function calculates the sorting sequence number value and ranking value of the specified data column in the dataframe, and assigns the ra
杰理之按键发起配对【篇】
一张图深入的理解FP/FN/Precision/Recall
R语言使用ggplot2函数可视化需要构建泊松回归模型的计数目标变量的直方图分布并分析构建泊松回归模型的可行性
杰理之发起对耳配对、回连、开启可发现、可连接的轮循函数【篇】
超分辨率技术在实时音视频领域的研究与实践
华南X99平台打鸡血教程
R language ggplot2 visualization: use the ggqqplot function of ggpubr package to visualize the QQ graph (Quantitative quantitative plot)
Matplotlib drawing 3D graphics
Uvalive – 4621 CAV greed + analysis "suggestions collection"
How to buy stocks on your mobile phone and open an account? Is it safe to open an account
State mode - Unity (finite state machine)
ASP.NET幼儿园连锁管理系统源码