[Leetcode]303.区域和检索&&304.二维区域和检索
1.区域和检索:
简单题,前缀和方法
乍一看就觉得应该用前缀和来做,一个数组多次查询。
实现方法: 新建一个private数组prefix_sum[i],用来存储nums前i个数组的和,
需要找区间和的时候直接通过prefix_sum[j]-prefix[i-1]即可得到从[i,j]区间的和,当i是0的时候需要特殊处理以防数组越界。
class NumArray {
public:
NumArray(vector<int> nums) {
prefix_sum.reserve(nums.size());
int sum = ;
for(int i: nums) {
sum+=i;
prefix_sum.push_back(sum);
}
}
int sumRange(int i, int j) {
if(i == ) return prefix_sum[j];
return prefix_sum[j]-prefix_sum[i-];
}
private:
vector<int> prefix_sum;
};
那我们来看一下,若是方阵的情况怎么办?
2.二维区域和检索
解决方法一样,不同点在于如何求和和如何通过前缀和获得解。
二维的从(row1,col1)~(row2,col2)的求和情况应该是
dp[row2][col2]+dp[row1-1][col1-1]-dp[row2][col1-1]-dp[row1-1][col2]
这个需要我们的一点点初中数学的知识,加的dp[row1][col1-1]是被重复删去的区间,所以要加回来。
同样,要避开那些边界特殊情况,直接用if条件筛掉就行了,细节观察注释。
class NumMatrix {
private: vector<vector<int>>dp;
public:
NumMatrix(vector<vector<int>> matrix) {
dp=matrix;
int n=matrix.size();
if(n>){
/*求和,先从左往右叠加*/
int m=matrix[].size();
for(int i=;i<n;i++)
for(int j=;j<m;j++)
dp[i][j]+=dp[i][j-];
/*再从上往下叠加*/
for(int i=;i<n;i++)
for(int j=;j<m;j++)
dp[i][j]+=dp[i-][j];
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
/*最特殊的情况:row1=0,col1=0*/
if(row1==&&col1==)return dp[row2][col2];
/*特殊情况1:row1=0但col1!=0*/
if(row1==){
return dp[row2][col2]-dp[row2][col1-];
}
/*特殊情况2:row1!=0但col1=0*/
else if(col1==){
return dp[row2][col2]-dp[row1-][col2];
}
/*正常情况:row1不等于0同时colq也不等于0*/
else{
return dp[row2][col2]+dp[row1-][col1-]-dp[row2][col1-]-dp[row1-][col2];
}
}
};
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/
[Leetcode]303.区域和检索&&304.二维区域和检索的更多相关文章
- Java实现 LeetCode 304 二维区域和检索 - 矩阵不可变
304. 二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). Range Sum Qu ...
- Leetcode 304.二维区域和检索-矩阵不可变
二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, c ...
- [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] 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之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- [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)二维区域和检索 个人题解
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...
- 304 Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...
随机推荐
- 2019.01.20 NOIP模拟 迅雷(kruskal/二分+并查集)
传送门 题意简述:给一张带权无向图,有a,ba,ba,b两类特殊点和普通点,问使得至少有一个aaa和一个bbb连通所需要的所有边边权最小值的最大值是多少. 思路: 一眼发现可以二分,考虑怎么check ...
- Le Chapitre VII
Le cinquième jour, toujours grâce au mouton, ce secrèt de la vie du petit prince me fut révélé. Il m ...
- DDR4控制笔记
DDR4接口 A[17:0] input 为激活命令提 供行地址,为读.写命令地址输入:提供列地址,也为模式寄存器设 置提供操作码,A[16]只用于8Gb和16Gb,A[17]只用于16Gb,另外 ...
- (转)ASP.NET MVC 第五个预览版和表单提交场景
转自:http://ourlife.blog.51cto.com/708821/296171 上个星期四,ASP.NET MVC开发团队发布了ASP.NET MVC框架的“第五个预览版”.你可以在这里 ...
- Html5与Css3知识点拾遗(一)
1.元素 空元素: 可选的空格空格和斜杠 <img src="x.jpg" width="300" alt="pic" /> & ...
- 【repost】DOM CRUD
//DOM 的 CRUD // c 创建create // 1.直接往body中动态的添加标签(可以是任意类型)document.write('helloWorld');document.write( ...
- Shell编程-09-Shell中的函数
目录 基本语法 函数执行 函数示例 函数可以简化程序的代码量,达到更好的代码复用度,因此会让程序变得更加易读.简洁和易修改.其作用就是将需要多次使用的代码整合到一块,使其成为一个整体,然后通过 ...
- (贪心 or DP)Woodcutters -- Codefor 545C
http://codeforces.com/contest/545/problem/C Woodcutters time limit per test 1 second memory limit p ...
- Java中方法重写和方法重载
首先方法重写和方法重载是建立在Java的面向对象的继承和多态的特性基础上而出现的.至于面向对象的继承和多态的特性我就不在这里多说了.继承是指在一个父类的基础再创建一个子类,这样子类就拥有了父类的非私 ...
- MySQL查询练习(45道)
题目:设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher). 四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1 ...