当前位置:网站首页>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));
}
}
边栏推荐
猜你喜欢
LeakCanary如何监听Service、Root View销毁时机?
xss-labs靶场挑战
用户体验 | 如何度量用户体验 ?
阿里腾讯面试一二
【钛晨报】国家统计局:7月制造业PMI为49%;玖富旗下理财产品涉嫌欺诈,涉及390亿元;国内航线机票燃油附加费8月5日0时起下调
Jenkins安装插件遇到的问题
图解MySQL内连接、外连接、左连接、右连接、全连接......太多了
Solve vscode input! Unable to quickly generate skeletons (three methods for the new version of vscode to quickly generate skeletons)
【likeshop】回收租凭系统100%开源无加密 商城+回收+租赁
报告:想学AI的学生数量已涨200%,老师都不够用了
随机推荐
CTFshow,命令执行:web37
机器学习 | MATLAB实现支持向量机回归RegressionSVM参数设定
Push the local project to the remote repository
将本地项目推送到远程仓库
Flutter Widget 如何启用和屏蔽点击事件
线上问题排查常用命令,总结太全了,建议收藏!!
Promise learning (4) The ultimate solution for asynchronous programming async + await: write asynchronous code in a synchronous way
浏览器快捷键大全
NIO‘s Sword(思维,取模,推公式)
redis6 跟着b站尚硅谷学习
用户体验 | 如何度量用户体验 ?
【无标题】
Android Security and Protection Policy
深度学习 | MATLAB实现GRU门控循环单元gruLayer参数设定
怎么找出电脑隐藏的软件(如何清理电脑隐藏软件)
Small application project works WeChat gourmet recipes applet graduation design of finished product (1) the development profile
C#/VB.NET convert PPT or PPTX to image
C语言实现!20000用4秒计算
使用KeyStore生成证书
小程序毕设作品之微信美食菜谱小程序毕业设计成品(2)小程序功能