领扣(LeetCode)二维区域和检索 个人题解
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。
上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8。
示例:
给定 matrix = [
[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]
] sumRegion(2, 1, 4, 3) -> 8
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12
说明:
- 你可以假设矩阵不可变。
- 会多次调用 sumRegion 方法。
- 你可以假设 row1 ≤ row2 且 col1 ≤ col2。
对于这一题,一开始拿到以为是很简单的,还想着为什么会放在中等题里面。
虽然知道可能会出现超时的问题,但是第一次写还是尝试了暴力遍历求区域内值的和。答案虽然是正确的,当然显而易见报错了。超时。
后来想到了(其实还是参考了思路,拜托欸,我可是萌新,哪里接触过这种空间换时间的神奇操作 XD),其实,每个区域块到左上角的值都可以简化为上一个已经处理过的区域块的加减乘除的取值,然后加上当前的值。同时,某个子区域的块也能转换成以上块的加减乘除集合。
公式为:
(略)
代码如下:
class NumMatrix {
int[][] matrix;
int[][] markmatr; public NumMatrix(int[][] matrix) {
this.matrix=matrix;
int x=matrix.length;
int y=x>0?matrix[0].length:0;
markmatr=new int[x+1][y+1];
for(int i=1;i<=x;i++)
{
for(int j=1;j<=y;j++)
{
markmatr[i][j]=markmatr[i-1][j]+markmatr[i][j-1]-markmatr[i-1][j-1]+matrix[i-1][j-1];
}
}
} public int sumRegion(int row1, int col1, int row2, int col2) {
int sum=0;
sum=markmatr[row2+1][col2+1]-markmatr[row1][col2+1]-markmatr[row2+1][col1]+markmatr[row1][col1];
return sum;
} }
领扣(LeetCode)二维区域和检索 个人题解的更多相关文章
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [Leetcode]303.区域和检索&&304.二维区域和检索
题目 1.区域和检索: 简单题,前缀和方法 乍一看就觉得应该用前缀和来做,一个数组多次查询. 实现方法: 新建一个private数组prefix_sum[i],用来存储nums前i个数组的和, 需要找 ...
- Leetcode 304.二维区域和检索-矩阵不可变
二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, c ...
- Java实现 LeetCode 304 二维区域和检索 - 矩阵不可变
304. 二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). Range Sum Qu ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] 304. Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- LeetCode 304. Range Sum Query 2D - Immutable 二维区域和检索 - 矩阵不可变(C++/Java)
题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- [Swift]LeetCode304. 二维区域和检索 - 矩阵不可变 | Range Sum Query 2D - Immutable
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [Swift]LeetCode308. 二维区域和检索 - 可变 $ Range Sum Query 2D - Mutable
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
随机推荐
- python-函数相关
一.函数: 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print(). 但你也可以自 ...
- bcache 状态/配置 文件详细介绍(翻译自官网)
声明: 文中 斜体带下划线 的段落为翻译不够准确的段落 原文:https://www.kernel.org/doc/Documentation/bcache.txt 官网:https://bcach ...
- boost::asio::tcp
同步TCP通信服务端 #include <boost/asio.hpp> #include <iostream> using namespace boost::asio; in ...
- python中使用logging将日志写入文件或输出到控制台
import logging import os class Logger: def __init__(self, name=__name__): # 创建一个loggger self.__name ...
- IDE安装完成无法打开,报错Fail load JVM DLL 问题与解决方案
安装完成pycharm 在打开pycharm的时候出现报错 Fail load JVM DLL xxxx xxx. 解决方案 安装Microsoft Visual C++ 2010 Redistrib ...
- iOS cocoapods导入项目 出现 "___gxx_personality_v0", referenced from: 或者 clang: error: linker command failed with exit code 1 (use -v to see invocation) 错误
今天想导入PNChart 编译的时候出现了 "___gxx_personality_v0", referenced from: 和 clang: error: linker c ...
- jenkins pipeline 流水线生产
jenkins pipeline : pipeline { agent any parameters { string(name: 'git_version', defaultValue: 'v1.1 ...
- springcloud之Feign、ribbon设置超时时间和重试机制的总结
一 超时时间配置 如果在一个微服务当中对同一个接口同时配置了Hystrix与ribbon两个超时时间,则在接口调用的时候,两个计时器会同时读秒. 比如,访问一个接口需要2秒,你的ribbon配置的超时 ...
- java和python对比
一:解释性和编译型 梳理 编译型:源代码经过编译直接变为二进制的机器语言,每次都可以直接重新运行不需要翻译.典型的就是c语言. 解释性:java和python都是解释型,源代码经过编译变为字节码文件, ...
- linux 安装swoole
1.首先我们要安装swoole扩展的话,需要把它的包下载下来,下载地址是: https://github.com/swoole/swoole-src 2.下载下来之后进行解压: unzip swool ...