当前位置:网站首页>Leetcode-304: two dimensional area and retrieval - matrix immutable
Leetcode-304: two dimensional area and retrieval - matrix immutable
2022-07-07 10:27:00 【Chrysanthemum headed bat】
leetcode-304: Two dimensional region and retrieval - The matrix is immutable
subject
Given a two-dimensional matrix matrix, Multiple requests of the following types :
- Calculate the sum of the elements in its subrectangle , Of the submatrix top left corner by (row1, col1) , The lower right corner by (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 top left corner (row1, col1) 、 The lower right corner (row2, col2) The elements of the described submatrix The sum of the .
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 )
Problem solving
Method 1 : The prefix and
preSums[i][j] The upper left corner is (0,0), The index in the lower right corner is (i,j) All elements in the matrix of and
1、 First, calculate preSums[i][j]
S ( O , D ) = S ( O , C ) + S ( O , B ) − S ( O , A ) + D S(O,D)=S(O,C)+S(O,B)−S(O,A)+D S(O,D)=S(O,C)+S(O,B)−S(O,A)+D
2、 According to the calculated preSums, You can get the sum of all the elements in the matrix you want to calculate
S ( A , D ) = S ( O , D ) − S ( O , E ) − S ( O , F ) + S ( O , G ) S(A,D)=S(O,D)−S(O,E)−S(O,F)+S(O,G) S(A,D)=S(O,D)−S(O,E)−S(O,F)+S(O,G)
class NumMatrix {
public:
vector<vector<int>> preSums;
NumMatrix(vector<vector<int>>& matrix) {
int m=matrix.size();
int n=matrix[0].size();
preSums.resize(m+1,vector<int>(n+1));
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
preSums[i+1][j+1]=preSums[i+1][j]+preSums[i][j+1]-preSums[i][j]+matrix[i][j];
}
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
return preSums[row2+1][col2+1]-preSums[row1][col2+1]-preSums[row2+1][col1]+preSums[row1][col1];
}
};
边栏推荐
- 【acwing】786. 第k个数
- Use of JSON extractor originals in JMeter
- AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
- 柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
- 大整数类实现阶乘
- .NET配置系统
- IIC基本知识
- OpenGL glLightfv 函数的应用以及光源的相关知识
- This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
- 电表远程抄表拉合闸操作命令指令
猜你喜欢

php \n 换行无法输出

Leetcode exercise - 113 Path sum II

【acwing】786. 第k个数

Appx code signing Guide

Remote meter reading, switching on and off operation command

XML configuration file parsing and modeling

The method of word automatically generating directory

【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码

IIC基本知识
![[sword finger offer] 42 Stack push in and pop-up sequence](/img/f4/eb69981163683c5b36f17992a87b3e.png)
[sword finger offer] 42 Stack push in and pop-up sequence
随机推荐
Download Text, pictures and ab packages used by unitywebrequest Foundation
P2788 数学1(math1)- 加减算式
Inno setup packaging and signing Guide
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
leetcode-560:和为 K 的子数组
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
1323:【例6.5】活动选择
PDF文档签名指南
Embedded background - chip
Can I open a stock trading account online? Is it safe
The method of word automatically generating directory
[牛客网刷题 Day6] JZ27 二叉树的镜像
2022.7.3DAY595
0x0fa23729 (vcruntime140d.dll) (in classes and objects - encapsulation.Exe) exception thrown (resolved)
STM32 product introduction
The request object parses the request body and request header parameters
Guid主键
【剑指Offer】42. 栈的压入、弹出序列
How to cancel automatic saving of changes in sqlyog database