当前位置:网站首页>Sword finger offer II 013 Sum of two-dimensional submatrix
Sword finger offer II 013 Sum of two-dimensional submatrix
2022-07-07 19:54:00 【Jin huaixuan】
Given a two-dimensional matrix matrix, Multiple requests of the following types :
Calculate the sum of the elements in its subrectangle , The upper left corner of the submatrix is (row1, col1) , The lower right corner is (row2, col2) .
Realization NumMatrix class :
NumMatrix(int[][] matrix) Given an integer matrix matrix To initialize
int sumRegion(int row1, int col1, int row2, int col2) Return to the upper left corner (row1, col1) 、 The lower right corner (row2, col2) The sum of the elements of the submatrix of .
Example 1:
Input :
["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]]
Output :
[null, 8, 11, 12]
explain :
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 ( The sum of the elements of the red rectangle )
numMatrix.sumRegion(1, 1, 2, 2); // return 11 ( The sum of the elements of the green rectangle )
numMatrix.sumRegion(1, 2, 2, 4); // return 12 ( Sum of elements in blue rectangle )
source : Power button (LeetCode)
link :https://leetcode.cn/problems/O4NDxx
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
java:
class NumMatrix {
public int[][] mat;
public NumMatrix(int[][] matrix) {
// Given a two-dimensional array , Initialize the two-dimensional array
// [[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;
// Find the value of the given subscript
for(int i = row1;i <= row2 ; i++){
for(int j = col1;j<=col2;j++){
total += this.mat[i][j];
}
}
return total;
}
}
边栏推荐
- Research and practice of super-resolution technology in the field of real-time audio and video
- Semantic SLAM源码解析
- Browse the purpose of point setting
- 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
- 开源重器!九章云极DataCanvas公司YLearn因果学习开源项目即将发布!
- 转置卷积理论解释(输入输出大小分析)
- LeetCode 648(C#)
- R language ggplot2 visualization: use the ggviolin function of ggpubr package to visualize the violin diagram, set the palette parameter to customize the filling color of violin diagrams at different
- 干货分享|DevExpress v22.1原版帮助文档下载集合
- 【剑指offer】剑指 Offer II 012. 左右两边子数组的和相等
猜你喜欢
谷歌seo外链Backlinks研究工具推荐
编译原理 实验一:词法分析器的自动实现(Lex词法分析)
模拟实现string类
LeetCode力扣(剑指offer 36-39)36. 二叉搜索树与双向链表37. 序列化二叉树38. 字符串的排列39. 数组中出现次数超过一半的数字
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
PMP practice once a day | don't get lost in the exam -7.7
8 CAS
转置卷积理论解释(输入输出大小分析)
CMD command enters MySQL times service name or command error (fool teaching)
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
随机推荐
LC:字符串转换整数 (atoi) + 外观数列 + 最长公共前缀
转置卷积理论解释(输入输出大小分析)
Key points of anti reptile: identifying reptiles
杰理之测试盒配置声道【篇】
what‘s the meaning of inference
【RT-Thread env 工具安装】
杰理之手动配对方式【篇】
841. 字符串哈希
ASP. Net gymnasium integrated member management system source code, free sharing
MySQL、sqlserver oracle数据库连接方式
剑指 Offer II 013. 二维子矩阵的和
编译器优化那些事儿(4):归纳变量
Redis master-slave and sentinel master-slave switchover are built step by step
Download from MySQL official website: mysql8 for Linux X Version (Graphic explanation)
AD域组策略管理
How to share the same storage among multiple kubernetes clusters
LeetCode 535(C#)
ASP.NET幼儿园连锁管理系统源码
线性基
ASP.NET体育馆综合会员管理系统源码,免费分享