当前位置:网站首页>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));
}
}
边栏推荐
- WPF 截图控件之绘制箭头(五)「仿微信」
- 我是如何保护 70000 ETH 并赢得 600 万漏洞赏金的
- STM32 personal notes - program run and fly
- 昇思大模型体验平台初体验——以小模型LeNet为例
- pgAdmin 4 v6.12 发布,PostgreSQL 开源图形化管理工具
- shell--面试题
- URL.createObjectURL、URL.revokeObjectURL、Uint8Array、Blob使用详解
- Introduction to STM32 development Introduce IIC bus, read and write AT24C02 (EEPROM) (using analog timing)
- 在线GC日志分析工具——GCeasy
- PDMan-domestic free general database modeling tool (minimalist, beautiful)
猜你喜欢
随机推荐
使用KeyStore生成证书
【无标题】
Jenkins安装插件遇到的问题
JWT
解决new Thread().Start导致高并发CPU 100%的问题
图解MySQL内连接、外连接、左连接、右连接、全连接......太多了
CTO strongly banning the use of the Calendar, that in what?
解决vscode输入! 无法快捷生成骨架(新版vscode快速生成骨架的三种方法)
小程序毕设作品之微信美食菜谱小程序毕业设计成品(4)开题报告
Stone Technology builds hard-core brand power and continues to expand the global market
July 31, 2022 -- Take your first steps with C# -- Use arrays and foreach statements in C# to store and iterate through sequences of data
小程序毕设作品之微信美食菜谱小程序毕业设计成品(1)开发概要
各位大拿,安装Solaris 11.4操作系统,在安装数据库依赖包的时候包这个错,目前无原厂支持,也无安装盘,联网下载后报这个错,请教怎么解决?
4种常见的鉴权方式及说明
xss漏洞学习
记一次 .NET 某智慧物流WCS系统CPU爆高分析
正则表达式
IntellJ IDEA如何显示换行符(line endings)
2022年7月31日--使用C#迈出第一步--使用 C# 创建具有约定、空格和注释的易读代码
Promise learning (4) The ultimate solution for asynchronous programming async + await: write asynchronous code in a synchronous way