当前位置:网站首页>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;
}
}
边栏推荐
- Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse
- Redis master-slave and sentinel master-slave switchover are built step by step
- ASP.NET幼儿园连锁管理系统源码
- R语言使用ggplot2函数可视化需要构建泊松回归模型的计数目标变量的直方图分布并分析构建泊松回归模型的可行性
- 8 CAS
- Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
- R语言dplyr包select函数、group_by函数、filter函数和do函数获取dataframe中指定因子变量中指定水平中特定数值数据列的值第三大的值
- L1-027 rental (Lua)
- R语言fpc包的dbscan函数对数据进行密度聚类分析、查看所有样本的聚类标签、table函数计算聚类簇标签与实际标签构成的二维列联表
- [Verilog advanced challenge of Niuke network question brushing series] ~ multi bit MUX synchronizer
猜你喜欢
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
最多可以参加的会议数目[贪心 + 优先队列]
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!!!
9 atomic operation class 18 Rohan enhancement
让这个 CRMEB 单商户微信商城系统火起来,太好用了!
索引总结(突击版本)
PV static creation and dynamic creation
Experiment 1 of Compilation Principle: automatic implementation of lexical analyzer (Lex lexical analysis)
模拟实现string类
[RT thread env tool installation]
随机推荐
[confluence] JVM memory adjustment
索引总结(突击版本)
2022如何评估与选择低代码开发平台?
银行理财产品怎么买?需要办银行卡吗?
【剑指offer】剑指 Offer II 012. 左右两边子数组的和相等
What does "true" mean
Make this crmeb single merchant wechat mall system popular, so easy to use!
9 原子操作类之18罗汉增强
ant desgin 多选
Introduction to bit operation
Key points of anti reptile: identifying reptiles
LeetCode力扣(剑指offer 36-39)36. 二叉搜索树与双向链表37. 序列化二叉树38. 字符串的排列39. 数组中出现次数超过一半的数字
杰理之发起对耳配对、回连、开启可发现、可连接的轮循函数【篇】
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
编译原理 实验一:词法分析器的自动实现(Lex词法分析)
PMP每日一练 | 考试不迷路-7.7
剑指 Offer II 013. 二维子矩阵的和
网信办公布《数据出境安全评估办法》,9 月 1 日起施行
Solve the problem of remote rviz error reporting
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