当前位置:网站首页>Sword finger offer II 013 Sum of two dimensional submatrix
Sword finger offer II 013 Sum of two dimensional submatrix
2022-06-10 00:50:00 【Small white yards fly up】
summary
Look at my picture , It is clear at a glance .
subject
link :https://leetcode.cn/problems/O4NDxx
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 .

Ideas
At first glance , The question is confused , Later, I found that his input and output were written in a mess .
The easiest way , It is based on the incoming row and column coordinates , The sum of the corresponding submatrix is traversed row by row .
Solution 1 : Regular traversal
Code
int[][] matrix;
public NumMatrix(int[][] matrix) {
this.matrix = matrix;
}
public int sumRegion(int row1, int col1, int row2, int col2) {
int result = 0;
for (int i = row1; i <= row2; i++) {
int[] matrix = this.matrix[i];
for (int j = col1; j <= col2; j++) {
result += matrix[j];
}
}
return result;
}
Solution 2 : Preprocessing matrix
Logic
Let's further optimize , Think about it , Can we follow the previous questions , By adding and the difference between them , Directly calculate the sum of submatrix ?
Look at the picture and find the pattern :

If we want to sum the blue submatrix , Then use the sum of the whole matrix - Sum of red frame submatrix - Sum of yellow frame submatrix + Sum of green submatrix , That's all right. .
So when we preprocess the matrix , You just need to traverse and calculate (0,0) To the sum of the area of the submatrix formed between each element , It can be used in the final calculation .
Be careful , Here, when dealing with , It is handled line by line .
Code
int[][] sumMatrix;
public NumMatrix(int[][] matrix) {
int[][] sumMatrix = new int[matrix.length + 1][matrix[0].length + 1];
for (int i = 0; i < matrix.length; i++) {
int rowSum = 0;
for (int j = 0; j < matrix[0].length; j++) {
rowSum += matrix[i][j];
sumMatrix[i + 1][j + 1] = sumMatrix[i][j + 1] + rowSum;
}
}
this.sumMatrix = sumMatrix;
}
public int sumRegion(int row1, int col1, int row2, int col2) {
return sumMatrix[row2 + 1][col2 + 1] - sumMatrix[row1][col2 + 1] - sumMatrix[row2 + 1][col1] + sumMatrix[row1][col1];
}
边栏推荐
- Application of DFS and BFS in binary tree
- 工藤正男:如何一年发表5篇SCI
- ECA-Net: Efficient Channel Attention for Deep Convolutional Neural Networks
- Cloudcompare & PCL principal curvature, mean curvature and Gaussian curvature calculation
- OSPF experiment
- How to set transparency for WPS text box
- Disorder of flinksql
- University of Ulm, Germany | comparative characterization of 3D protein structure
- Several syntax for adding events to elements in a page
- RHCSA第二天
猜你喜欢
Weights of complete binary tree of past real questions [10th] [provincial competition] [group B]

剑指 Offer II 011. 0 和 1 个数相同的子数组
力扣 旋转字符串 C语言 题解

RHCSA第三天

Mat数据的深浅拷贝

JS logic empty allocation double question mark syntax, double vertical bar syntax and optional chain syntax

University of Ulm, Germany | comparative characterization of 3D protein structure
Score of sub series of previous test questions and [11th] [provincial competition] [group B]

ECA-Net: Efficient Channel Attention for Deep Convolutional Neural Networks

if判断是否为空时的函数选择
随机推荐
Solution to C language problems of force buckle rotation string
IDC authority predicts that China's manufacturing industry will soon take advantage of the cloud
1049 robber Ah Fu
BGP协议实验
JS logic empty allocation double question mark syntax, double vertical bar syntax and optional chain syntax
flutter pub get failed (66; Could not find a file named “pubspec.yaml“
剑指 Offer II 010. 和为 k 的子数组
剑指 Offer II 018. 有效的回文
[GoogleCTF2019 Quals]Bnv -S
Batch deletion function - background mapper layer SQL statement parsing
46 year old new academician: when I was a graduate student, I unloaded all the Games
工程伦理课后习题答案
run npm fund for details
Application of DFS and BFS in binary tree
BS based_ Pagination query function of pagination plug-in
Syntaxe des points d'interrogation doubles, syntaxe des barres verticales doubles et syntaxe des chaînes optionnelles pour l'attribution logique de l'espace JS
BGP实验
Sélection de la fonction pour déterminer si elle est vide
Facial Emotion Recognition: State of the Art Performance on FER2013
试题 历届试题 子串分值和【第十一届】【省赛】【B组】