力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

暴力
双层for循环,遍历子矩阵。
前缀和算法:
- class NumMatrix {
- private int sum[][];
- private int matrix[][];
- public NumMatrix(int[][] matrix) {
- this.matrix=matrix;
- int rows=matrix.length+1;
- int cols=matrix[0].length+1;
- this.sum=new int[rows][cols];
- init(cols,rows);
- }
- public void init(int cols,int rows)
- {
- for(int i=1;i
- {
- for(int j=1;j
- {
- this.sum[i][j]=this.sum[i-1][j]+this.sum[i][j-1]-this.sum[i-1][j-1]+this.matrix[i-1][j-1];
- }
- }
-
- }
-
- public int sumRegion(int row1, int col1, int row2, int col2) {
- return this.sum[row2+1][col2+1]-this.sum[row2+1][col1]-this.sum[row1][col2+1]+this.sum[row1][col1];
- }
- }
-
- /**
- * Your NumMatrix object will be instantiated and called as such:
- * NumMatrix obj = new NumMatrix(matrix);
- * int param_1 = obj.sumRegion(row1,col1,row2,col2);
- */
-
相关阅读:
【数据加密、解密】前后端数据传输的过程中,如何进行数据加密传输,保证数据的传输安全,防止被他人窃取
工业产品外观设计的4个关键因素,你知道吗?
基于java+SpringBoot+VUE+Mysql社区家庭医生服务系统
CListCtrl设置只显示单列 2023/9/5 下午4:07:05
网络协议之:redis protocol 详解
Socket数据报套接字
[云] 大数据分析栈(Big Data Analytics Stack)+ Apache Hadoop分布式文件系统(HDFS)+Apache Spark
sqli-labs/Less-52
【漏洞复现】WordPress插件wp-file-manager任意文件上传漏洞(CVE-2020-25213)
Linux下的系统编程——线程同步(十三)
-
原文地址:https://blog.csdn.net/m0_71385141/article/details/132776337