当前位置:网站首页>leetcode/子矩阵元素和
leetcode/子矩阵元素和
2022-08-01 10:55:00 【xcrj】
思路
二维前缀和
代码
package com.xcrj;
/** * 给定一个二维矩阵 matrix,计算其子矩形范围内元素的总和 * 已知:该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2) 。 */
public class Solutions13 {
/** * 一维前缀和 * preSum[j]+k=preSum[i], k是子矩阵行和 */
static class NumMatrix1 {
// 所有行前缀和
int[][] preSum;
public NumMatrix1(int[][] matrix) {
// 所有行前缀和
preSum = new int[matrix.length][matrix[0].length + 1];
// 没有任何元素是前缀和为0
for (int i = 0; i < matrix.length; i++) {
preSum[i][0] = 0;
}
// 求所有行前缀和
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
// preSum[i][1], 行前缀和有1个元素时
preSum[i][j + 1] = preSum[i][j] + matrix[i][j];
}
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
int sumK = 0;
// 遍历所有行 求行前缀和 的和
for (int i = row1; i <= row2; i++) {
// 每行的前缀和
sumK += preSum[i][col2 + 1] - preSum[i][col1];
}
return sumK;
}
}
/** * 二维前缀和 * 2*preSum[i][j]+k=preSum[i][n]+preSUm[m][j] */
static class NumMatrix2 {
// 二维前缀和
int[][] preSum;
public NumMatrix2(int[][] matrix) {
// 所有行前缀和
preSum = new int[matrix.length + 1][matrix[0].length + 1];
// 没有任何元素是前缀和为0
preSum[0][0] = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
// - preSum[i][j]原因 preSum[i][j + 1] 和 preSum[i + 1][j]都含有preSum[i][j]
preSum[i + 1][j + 1] = preSum[i][j + 1] + preSum[i + 1][j] - preSum[i][j] + matrix[i][j];
}
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
// - preSum[row2 + 1][col1] - preSum[row1][col2 + 1] 因为都包含preSum[row1][col1]所以多减去了1个preSum[row1][col1]
return preSum[row2 + 1][col2 + 1] - preSum[row2 + 1][col1] - preSum[row1][col2 + 1] + preSum[row1][col1];
}
}
public static void main(String[] args) {
// 一维前缀和
NumMatrix1 numMatrix1 = new NumMatrix1(new int[][]{
{
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}});
// 输出8
System.out.println(numMatrix1.sumRegion(2, 1, 4, 3));
// 二维前缀和
NumMatrix2 numMatrix2 = new NumMatrix2(new int[][]{
{
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}});
// 输出8
System.out.println(numMatrix2.sumRegion(2, 1, 4, 3));
}
}
边栏推荐
- C#/VB.NET 将PPT或PPTX转换为图像
- 昇思大模型体验平台初体验——以小模型LeNet为例
- Introduction to data warehouse layering (real-time data warehouse architecture)
- RK3399平台开发系列讲解(内核入门篇)1.52、printk函数分析 - 其函数调用时候会关闭中断
- 在线GC日志分析工具——GCeasy
- Mini Program Graduation Works WeChat Food Recipes Mini Program Graduation Design Finished Products (2) Mini Program Functions
- 从零开始Blazor Server(4)--登录系统
- Endorsed in 2022 years inventory | product base, science and technology, guangzhou automobile group striding forward
- Flutter Widget 如何启用和屏蔽点击事件
- Golang内存分析工具gctrace和pprof实战
猜你喜欢

图解MySQL内连接、外连接、左连接、右连接、全连接......太多了

CTO strongly banning the use of the Calendar, that in what?

.NET性能优化-使用SourceGenerator-Logger记录日志

Promise learning (2) An article takes you to quickly understand the common APIs in Promise

招聘随想2022

世界第4疯狂的科学家,在103岁生日那天去世了

冰冰学习笔记:gcc、gdb等工具的使用

Mysql索引相关的知识复盘一

What is a stepper motor?40 pictures to show you!

C#/VB.NET convert PPT or PPTX to image
随机推荐
监视网络连接的ss命令
DBPack SQL Tracing 功能及数据加密功能详解
报告:想学AI的学生数量已涨200%,老师都不够用了
7. SAP ABAP OData 服务如何支持 $orderby (排序)操作
跨域网络资源文件下载
小程序毕设作品之微信美食菜谱小程序毕业设计成品(3)后台功能
退役划水
STM32 Personal Notes - Watchdog
回归预测 | MATLAB实现TPA-LSTM(时间注意力注意力机制长短期记忆神经网络)多输入单输出
C#/VB.NET convert PPT or PPTX to image
Google Earth Engine APP——15行代码搞定一个inspector高程监测APP
7/31 训练日志
将本地项目推送到远程仓库
解决vscode输入! 无法快捷生成骨架(新版vscode快速生成骨架的三种方法)
上周热点回顾(7.25-7.31)
retired paddling
如何从完美的智能合约中窃取 1 亿美元
Dapr 与 NestJs ,实战编写一个 Pub & Sub 装饰器
.NET性能优化-使用SourceGenerator-Logger记录日志
IntellJ IDEA如何显示换行符(line endings)