【刷题-LeetCode】304. Range Sum Query 2D - Immutable
- Range Sum Query 2D - Immutable
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.
Example:
Given 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
Note:
- You may assume that the matrix does not change.
- There are many calls to sumRegion function.
- You may assume that row1 ≤ row2 and col1 ≤ col2.
解 同一维数组一样,先预处理
定义数组\(\mathrm{S}[i][j]\)表示前 i-1 行前 j-1 列交叉区域的和
预处理阶段:\(\mathrm{S}[i][j] =\mathrm{M}[i-1][j-1]\mathrm{S}[i][j-1]+\mathrm{S}[i-1][j] - \mathrm{S}[i-1][j-1]\)
查询阶段:\(\mathrm{sum\_of\_region}[r1, c1, r2, c2] = \mathrm{S}[r2+1][c2+1]-\mathrm{S}[r1][c2+1]-\mathrm{S}[r2+1][c1]+\mathrm{S}[r1][c1]\)
class NumMatrix {
public:
vector<vector<int>>S;
NumMatrix(vector<vector<int>>& matrix) {
int m = matrix.size();
if(m > 0){
int n = matrix[0].size();
S.resize(m+1, vector<int>(n+1, 0));
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
S[i+1][j+1] = matrix[i][j] + S[i][j+1]+S[i+1][j] - S[i][j];
}
}
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
if(S.size() == 0)return 0;
return S[row2+1][col2+1] - S[row1][col2+1] - S[row2+1][col1] + S[row1][col1];
}
};
【刷题-LeetCode】304. Range Sum Query 2D - Immutable的更多相关文章
- [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二维区间求和 - 不变
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
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 ...
- 【LeetCode】304. Range Sum Query 2D - Immutable 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 预先求和 相似题目 参考资料 日期 题目地址:htt ...
- 304. Range Sum Query 2D - Immutable
题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- 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 ...
- 304 Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...
随机推荐
- TensorFlow.NET机器学习入门【1】开发环境与类型简介
项目开发环境为Visual Studio 2019 + .Net 5 创建新项目后首先通过Nuget引入相关包: SciSharp.TensorFlow.Redist是Google提供的TensorF ...
- CF981B Businessmen Problems 题解
Content 有一个长度为 \(n\) 的序列和长度为 \(m\) 的序列,两个序列中的元素都有一个编号 \(num\) 和一个值 \(val\),且同一个序列的元素之间的编号互不相同.现在从这两个 ...
- RabbitMQ 消息队列 实现RPC 远程过程调用交互
#!/usr/bin/env python # Author:Zhangmingda import pika,time import uuid class FibonacciRpcClient(obj ...
- Chrome的强大搜索功能
前言 前几天一个好朋友求助我,大概问题是他的电脑QQ啥都能上网,就浏览器上不了网不是IE而是chrome,我第一反应可能是dns问题.后来发甩过来一张图,好家伙把我吓得,类似于下面这张图 这图是我自己 ...
- JAVA实现查询栏目、类别(菜单)返回树结构(递归)
其中Channel.java是栏目结构,ChannelDto是我自己封装的返回给前端的数据,可以根据自己的来 这个的逻辑就是双重循环遍历每个类别,然后判断如果当前类别是其他类别的父级,那么就把其他类别 ...
- JS将时间戳转换为日期格式
function getDate(time){ var date =(new Date(parseInt(time))).toLocaleDateString() return date; } tim ...
- 【LeetCode】598. Range Addition II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- MySQL 中 count(*) 和 count(1)
一张有 100W 条数据的表 CREATE TABLE `user` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `username` var ...
- nginx -g "daemon off;" 你学废了吗?
去年的时候写了一篇原创<前后端分离,如何在前端项目中动态插入后端API基地址?(in docker)>, 我自认为这篇生产实践是对大前端. 容器化.CI/CD的得意之作. 对于前后端分离的 ...
- MongoDB应用场景及选型
1. MongoDB数据库定位 * OLTP数据库 * 原则上Oracle和MySQL能做得事情,MongoDB都能做(包括ACID事务) * 优点:横向扩展能力,数据量或并发量增加时候可以自动扩展 ...