当前位置:网站首页>[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); */
边栏推荐
- First use of dosbox
- Excel导入,导出功能实现
- Teach you to release a DeNO module hand in hand
- [offer18] delete the node of the linked list
- [算法] 剑指offer2 golang 面试题10:和为k的子数组
- Unity3D制作注册登录界面,并实现场景跳转
- The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
- Unity3D,阿里云服务器,平台配置
- GNSS定位精度指标计算
- What are the advantages of using SQL in Excel VBA
猜你喜欢
[算法] 剑指offer2 golang 面试题2:二进制加法
使用rtknavi进行RT-PPP测试
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
Combination of fairygui check box and progress bar
Office提示您的许可证不是正版弹框解决
Esp8266 connect onenet (old mqtt mode)
堆排序【手写小根堆】
Idea problem record
Expected value (EV)
Fairygui gain buff value change display
随机推荐
Excel导入,导出功能实现
Meanings and differences of PV, UV, IP, VV, CV
基本Dos命令
Esp8266 connect onenet (old mqtt mode)
FairyGUI人物状态弹窗
Force buckle 1189 Maximum number of "balloons"
Fabrication of fairygui simple Backpack
[offer9] implement queues with two stacks
【RTKLIB 2.4.3 b34 】版本更新简介一
Compile GDAL source code with nmake (win10, vs2022)
[leetcode622] design circular queue
[Chongqing Guangdong education] Shandong University College Physics reference materials
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
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
Introduction to the daily practice column of the Blue Bridge Cup
RTKLIB: demo5 b34f.1 vs b33
2021.11.10 compilation examination
最短Hamilton路径 (状压DP)
FairyGUI簡單背包的制作
SSD technical features