当前位置:网站首页>剑指 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;
}
}边栏推荐
- R language ggplot2 visualization: use the ggdensity function of ggpubr package to visualize the packet density graph, and use stat_ overlay_ normal_ The density function superimposes the positive dist
- R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化分组密度图、使用stat_overlay_normal_density函数为每个分组的密度图叠加正太分布曲线
- Tips and tricks of image segmentation summarized from 39 Kabul competitions
- 网信办公布《数据出境安全评估办法》,9 月 1 日起施行
- 浏览积分设置的目的
- R语言ggplot2可视化:使用ggpubr包的ggecdf函数可视化分组经验累积密度分布函数曲线、linetype参数指定不同分组曲线的线型
- 【RT-Thread env 工具安装】
- how to prove compiler‘s correctness
- 注解。。。
- IP 工具类
猜你喜欢

Dynamic addition of El upload upload component; El upload dynamically uploads files; El upload distinguishes which component uploads the file.

Numpy——2. Shape of array

【牛客网刷题系列 之 Verilog进阶挑战】~ 多bit MUX同步器

9 原子操作类之18罗汉增强

Download from MySQL official website: mysql8 for Linux X Version (Graphic explanation)

一张图深入的理解FP/FN/Precision/Recall

Experiment 1 of Compilation Principle: automatic implementation of lexical analyzer (Lex lexical analysis)

Le PGR est - il utile au travail? Comment choisir une plate - forme fiable pour économiser le cœur et la main - d'œuvre lors de la préparation de l'examen!!!

ASP.NET幼儿园连锁管理系统源码

9 atomic operation class 18 Rohan enhancement
随机推荐
“本真”是什么意思
杰理之快速配对,不支持取消配对【篇】
2022.07.05
2022如何评估与选择低代码开发平台?
L1-028 judging prime number (Lua)
杰理之相同声道的耳机不允许配对【篇】
Empowering smart power construction | Kirin Xin'an high availability cluster management system to ensure the continuity of users' key businesses
【牛客网刷题系列 之 Verilog进阶挑战】~ 多bit MUX同步器
Solve the problem of remote rviz error reporting
2022.07.04
小试牛刀之NunJucks模板引擎
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
Kirin Xin'an won the bid for the new generation dispatching project of State Grid!
指定opencv非标准安装的版本
R语言fpc包的dbscan函数对数据进行密度聚类分析、查看所有样本的聚类标签、table函数计算聚类簇标签与实际标签构成的二维列联表
8 CAS
UCloud是基础云计算服务提供商
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的填充色、add参数在小提琴图添加箱图
Throughput
PV静态创建和动态创建