当前位置:网站首页>[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
2022-07-06 12:51:00 【Deng Jiawen jarvan】
[ Algorithm ] The finger of the sword offer2 golang Interview questions 13: Sum of numbers of two-dimensional submatrix
subject 1:
Ideas 1: Violent simulation
Violent simulation
Code
type NumMatrix struct {
Matrix [][]int
}
func Constructor(matrix [][]int) NumMatrix {
return NumMatrix{
Matrix: matrix}
}
//start: 17:05
// Sum of elements of two-dimensional submatrix
// Ideas : Traverse each row of the submatrix and accumulate values
func (this *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
// Processing parameters todo
// matrix
// That's ok i, Column j
sum := 0
for i:=row1; i <= row2; i++ {
for j:=col1; j <= col2; j++{
sum += this.Matrix[i][j]
}
}
return sum
}
/** * Your NumMatrix object will be instantiated and called as such: * obj := Constructor(matrix); * param_1 := obj.SumRegion(row1,col1,row2,col2); */
Ideas 2: Calculate the value of each area in advance, and then finally calculate
For example, calculate the blue area 1 The value of can be equal to the red area 2 Subtract the green area 3 And other areas 4 and 5
When we construct, we record the values of the upper left to lower right positions of the rectangle, and then obtain the time complexity of each region O(1)
Code 2
type NumMatrix struct {
sums [][]int
}
func Constructor(matrix [][]int) NumMatrix {
// first line
for j:=1; j < len(matrix[0]); j++{
matrix[0][j] = matrix[0][j-1] + matrix[0][j]
}
// The first 2-n That's ok
for i:=1; i < len(matrix); i++ {
sum := 0
for j:=0; j < len(matrix[0]); j++{
sum += matrix[i][j]
matrix[i][j] = sum + matrix[i-1][j]
}
}
return NumMatrix{
sums: matrix}
}
//start: 17:05
// Sum of elements of two-dimensional submatrix
// Ideas 2: Calculate ahead of time (0,0) To each point sum Then the second calculation can O(1)
//a1 - a2 - a3 -a4
func (this *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
a1 := this.sums[row2][col2]
a2 := 0
a3 := 0
a4 := 0
if row1 >= 1 {
a2 = this.sums[row1-1][col1]
if col1 >= 1 {
a2 = this.sums[row1-1][col1-1]
}
}
if row1 >= 1 {
a3 = this.sums[row1-1][col2] - a2
}
if col1 >= 1 {
a4 = this.sums[row2][col1-1] - a2
}
return a1 - a2 - a3 - a4
}
/** * Your NumMatrix object will be instantiated and called as such: * obj := Constructor(matrix); * param_1 := obj.SumRegion(row1,col1,row2,col2); */
边栏推荐
- Unity3D,阿里云服务器,平台配置
- Vulnhub target: hacknos_ PLAYER V1.1
- [算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
- What are the functions and features of helm or terrain
- Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
- First use of dosbox
- (5) Introduction to R language bioinformatics -- ORF and sequence analysis
- Mixed use of fairygui button dynamics
- FGUI工程打包发布&导入Unity&将UI显示出来的方式
- 基本Dos命令
猜你喜欢
堆排序【手写小根堆】
【干货】提升RTK模糊度固定率的建议之周跳探测
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
Programming homework: educational administration management system (C language)
NRF24L01 troubleshooting
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
Unity3D,阿里云服务器,平台配置
idea中导包方法
dosbox第一次使用
(core focus of software engineering review) Chapter V detailed design exercises
随机推荐
(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
Force buckle 1189 Maximum number of "balloons"
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
Introduction to the daily practice column of the Blue Bridge Cup
Unity3d makes the registration login interface and realizes the scene jump
Esp8266 connect onenet (old mqtt mode)
(1) Introduction Guide to R language - the first step of data analysis
(core focus of software engineering review) Chapter V detailed design exercises
The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
Derivation of logistic regression theory
Unity3D,阿里云服务器,平台配置
Knowledge system of digital IT practitioners | software development methods -- agile
基本Dos命令
[leetcode622] design circular queue
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
Fabrication of fairygui simple Backpack
Solution to the problem of automatic login in Yanshan University Campus Network
Combination of fairygui check box and progress bar
[899]有序队列