当前位置:网站首页>剑指 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;
}
}
边栏推荐
- Solve the error reporting problem of rosdep
- LeetCode 515(C#)
- Research and practice of super-resolution technology in the field of real-time audio and video
- 微信公众号OAuth2.0授权登录并显示用户信息
- 【Confluence】JVM内存调整
- Number - number (Lua)
- Uvalive – 4621 CAV greed + analysis "suggestions collection"
- Key points of anti reptile: identifying reptiles
- 谷歌seo外链Backlinks研究工具推荐
- 杰理之关于 TWS 交叉配对的配置【篇】
猜你喜欢
最多可以参加的会议数目[贪心 + 优先队列]
爬虫实战(七):爬王者英雄图片
Interpretation of transpose convolution theory (input-output size analysis)
Pasqal首席技术官:模拟量子计算率先为工业带来量子优势
Seize Jay Chou
Jerry's headphones with the same channel are not allowed to pair [article]
小试牛刀之NunJucks模板引擎
Empowering smart power construction | Kirin Xin'an high availability cluster management system to ensure the continuity of users' key businesses
J ü rgen schmidhub reviews the 25th anniversary of LSTM papers: long short term memory All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversar
Responsibility chain model - unity
随机推荐
时间工具类
一张图深入的理解FP/FN/Precision/Recall
LC:字符串转换整数 (atoi) + 外观数列 + 最长公共前缀
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!!!
现在股票开户可以直接在网上开吗?安全吗。
Automatic classification of defective photovoltaic module cells in electroluminescence images-论文阅读笔记
8 CAS
爬虫实战(七):爬王者英雄图片
[confluence] JVM memory adjustment
Key points of anti reptile: identifying reptiles
“本真”是什么意思
9 原子操作类之18罗汉增强
ASP. Net kindergarten chain management system source code
位运算介绍
Numpy——axis
LeetCode 535(C#)
PMP对工作有益吗?怎么选择靠谱平台让备考更省心省力!!!
The strength index of specialized and new software development enterprises was released, and Kirin Xin'an was honored on the list
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
tp6 实现佣金排行榜