当前位置:网站首页>[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
2022-07-06 09:18:00 【邓嘉文Jarvan】
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
题目1:

思路1: 暴力模拟
暴力模拟
代码
type NumMatrix struct {
Matrix [][]int
}
func Constructor(matrix [][]int) NumMatrix {
return NumMatrix{
Matrix: matrix}
}
//start: 17:05
//二维子矩阵的元素和
//思路: 遍历子矩阵的每一行累加值即可
func (this *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
//参数处理 todo
//矩阵
//行i,列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); */

思路2: 提前算好每个区域的值然后最后计算
比如计算蓝色区域1的值可以等于红色区域2减去绿色区域3和其他区域4和5
我们在构造的时候记录好矩形左上到右下位置的值然后获得每个区域的时候时间复杂度是 O(1)

代码2
type NumMatrix struct {
sums [][]int
}
func Constructor(matrix [][]int) NumMatrix {
//第一行
for j:=1; j < len(matrix[0]); j++{
matrix[0][j] = matrix[0][j-1] + matrix[0][j]
}
//第2-n行
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
//二维子矩阵的元素和
//思路2: 提前计算好(0,0)到每个点的sum然后第二次计算就能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); */

边栏推荐
- Special palindromes of daily practice of Blue Bridge Cup
- FairyGUI条子家族(滚动条,滑动条,进度条)
- (四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
- [offer78] merge multiple ordered linked lists
- 编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
- JS function promotion and declaration promotion of VaR variable
- [Offer18]删除链表的节点
- [899] ordered queue
- 【干货】提升RTK模糊度固定率的建议之周跳探测
- Unity3d makes the registration login interface and realizes the scene jump
猜你喜欢

程序设计大作业:教务管理系统(C语言)

ORA-02030: can only select from fixed tables/views

微信小程序开发心得

Theoretical derivation of support vector machine

(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析

Prove the time complexity of heap sorting

(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart

Unity3D制作注册登录界面,并实现场景跳转
![Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]](/img/b0/176bf6dea2201afc892d6750c5974b.png)
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]

Database course design: college educational administration management system (including code)
随机推荐
JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
Easy to use shortcut keys in idea
KF UD分解之伪代码实现进阶篇【2】
【GNSS】抗差估计(稳健估计)原理及程序实现
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
Mixed use of fairygui button dynamics
Unity3d, Alibaba cloud server, platform configuration
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
FairyGUI人物状态弹窗
JS function promotion and declaration promotion of VaR variable
Gateway fails to route according to the service name, and reports an error service unavailable, status=503
Get the position of the nth occurrence of the string
Unity3D摄像机,键盘控制前后左右上下移动,鼠标控制旋转、放缩
ESP8266连接onenet(旧版MQTT方式)
PRIDE-PPPAR源码解析
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
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
Prove the time complexity of heap sorting
(课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)