当前位置:网站首页>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));
}
}
边栏推荐
- 微信公众号授权登录后报redirect_uri参数错误的问题
- NIO‘s Sword(思维,取模,推公式)
- CTFshow,命令执行:web31
- 玻璃拟态(Glassmorphism)设计风格
- Kaitian aPaaS mobile phone number empty number detection [Kaitian aPaaS battle]
- STM32入门开发 介绍IIC总线、读写AT24C02(EEPROM)(采用模拟时序)
- 2022年7月31日--使用C#迈出第一步--使用 C# 创建具有约定、空格和注释的易读代码
- 招聘随想2022
- 数仓分层简介(实时数仓架构)
- 语音聊天app源码——语音聊天派对
猜你喜欢

Promise learning (1) What is Promise?how to use?How to solve callback hell?

LeakCanary如何监听Service、Root View销毁时机?

xss-labs靶场挑战

阿里腾讯面试一二

Promise学习(四)异步编程的终极解决方案async + await:用同步的方式去写异步代码

DBPack SQL Tracing 功能及数据加密功能详解

如何从完美的智能合约中窃取 1 亿美元

MacOS下postgresql(pgsql)数据库密码为什么不需要填写或可以乱填写

Mini Program Graduation Works WeChat Food Recipes Mini Program Graduation Design Finished Products (3) Background Functions

爱可可AI前沿推介(8.1)
随机推荐
C#/VB.NET 将PPT或PPTX转换为图像
gc的意义和触发条件
Promise学习(四)异步编程的终极解决方案async + await:用同步的方式去写异步代码
Flutter Widget 如何启用和屏蔽点击事件
JWT
分类预测 | MATLAB实现1-DCNN一维卷积神经网络分类预测
Solve vscode input! Unable to quickly generate skeletons (three methods for the new version of vscode to quickly generate skeletons)
上周热点回顾(7.25-7.31)
广域铭岛入选2022年重庆市数字经济产业发展试点示范项目名单
【likeshop】回收租凭系统100%开源无加密 商城+回收+租赁
退役划水
RK3399 platform development series on introduction to (kernel) 1.52, printk function analysis - the function call will be closed
July 31, 2022 -- Take your first steps with C# -- Use arrays and foreach statements in C# to store and iterate through sequences of data
NIO‘s Sword(思维,取模,推公式)
昇思大模型体验平台初体验——以小模型LeNet为例
小程序毕设作品之微信美食菜谱小程序毕业设计成品(2)小程序功能
WTM:ASP.NET Core快速开发利器!
Promise学习(一)Promise是什么?怎么用?回调地狱怎么解决?
WPF 截图控件之绘制箭头(五)「仿微信」
每日一题:连续子数组的最大和(动态规划)