当前位置:网站首页>leetcode/submatrix element sum
leetcode/submatrix element sum
2022-08-01 11:03:00 【xcrj】
思路
二维前缀和
代码
package com.xcrj;
/** * 给定一个二维矩阵 matrix,计算其子矩形范围内元素的总和 * 已知:该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2) . */
public class Solutions13 {
/** * 一维前缀和 * preSum[j]+k=preSum[i], kIs the child matrix row and */
static class NumMatrix1 {
// All rows prefix and
int[][] preSum;
public NumMatrix1(int[][] matrix) {
// All rows prefix and
preSum = new int[matrix.length][matrix[0].length + 1];
// There is no any element prefix and as0
for (int i = 0; i < matrix.length; i++) {
preSum[i][0] = 0;
}
// For all the rows prefix and
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
// preSum[i][1], Prefix and have1个元素时
preSum[i][j + 1] = preSum[i][j] + matrix[i][j];
}
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
int sumK = 0;
// 遍历所有行 O line prefix and 的和
for (int i = row1; i <= row2; i++) {
// Each row of the prefix and
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) {
// All rows prefix and
preSum = new int[matrix.length + 1][matrix[0].length + 1];
// There is no any element prefix and as0
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] Because containpreSum[row1][col1]So many lost1个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));
}
}
边栏推荐
- 昇思大模型体验平台初体验——以小模型LeNet为例
- [Nodejs] node的fs模块
- STM32 personal notes - program run and fly
- URL.createObjectURL、URL.revokeObjectURL、Uint8Array、Blob使用详解
- Promise learning (1) What is Promise?how to use?How to solve callback hell?
- 进制与转换、关键字
- Online - GCeasy GC log analysis tools
- MacOS下postgresql(pgsql)数据库密码为什么不需要填写或可以乱填写
- EasyRecovery热门免费数据检测修复软件
- 回归预测 | MATLAB实现TPA-LSTM(时间注意力注意力机制长短期记忆神经网络)多输入单输出
猜你喜欢
随机推荐
Basic configuration commands of cisco switches (what is the save command of Huawei switches)
昇思大模型体验平台初体验——以小模型LeNet为例
WPF 截图控件之绘制箭头(五)「仿微信」
新书上市 |《谁在掷骰子?》在“不确定性时代”中确定前行
Why Metropolis–Hastings Works
各位大拿,安装Solaris 11.4操作系统,在安装数据库依赖包的时候包这个错,目前无原厂支持,也无安装盘,联网下载后报这个错,请教怎么解决?
C#/VB.NET convert PPT or PPTX to image
轮询和长轮询的区别
July 31, 2022 -- Take your first steps with C# -- Use arrays and foreach statements in C# to store and iterate through sequences of data
July 31, 2022 -- Take your first steps with C# -- Use C# to create readable code with conventions, spaces, and comments
Browser shortcut keys
DBPack SQL Tracing 功能及数据加密功能详解
ACL 2022 | 文本生成的相关前沿进展
如何在IntellJ IDEA中批量修改文件换行符
redis6 跟着b站尚硅谷学习
Android Security and Protection Policy
大众碰到点评的一个字体反爬,落地技术也是绝了
2022年7月31日--使用C#迈出第一步--使用 C# 创建具有约定、空格和注释的易读代码
PDMan-国产免费通用数据库建模工具(极简,漂亮)
slice、splice、split傻傻分不清