题目

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.二维区域和检索的更多相关文章

  1. Java实现 LeetCode 304 二维区域和检索 - 矩阵不可变

    304. 二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). Range Sum Qu ...

  2. Leetcode 304.二维区域和检索-矩阵不可变

    二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, c ...

  3. [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 ...

  4. [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 ...

  5. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  6. [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 ...

  7. 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 ...

  8. 领扣(LeetCode)二维区域和检索 个人题解

    给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...

  9. 304 Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...

随机推荐

  1. pat1079+1086+1090+1094(树的遍历)感想

    今天做了这4道题,虽然大部分以前做过,但还是有些知识掌握不全. 总结一下所用的树的知识及解决方法 (1)非二叉树的遍历: 非二叉树就是图,所以它的存储结构类似邻接表,c++提供了vector数组可以很 ...

  2. java equals重写

    @Override    public boolean equals(Object obj) {        if(this == obj) {            return true;   ...

  3. IntelliJ IDEA 2017版 spring-boot2.0.2 自动配置Condition

    描述: 编译器修改参数      -Dfile.encoding=GBK     -Dstr.encoding=GBK Condition位置: 某一个类或注解存在的时候,装配,否则不装配 相关代码: ...

  4. C语言的问题,头文件:keil也许有漏洞

    2018-06-15   16:52:03 ------------------------------------------------------------------------------ ...

  5. p1 批梯度下降算法

    (蓝色字体:批注:绿色背景:需要注意的地方:橙色背景是问题) 一,机器学习分类 二,梯度下降算法:2.1模型   2.2代价函数   2.3 梯度下降算法 一,机器学习分类 无监督学习和监督学习 无监 ...

  6. 第21章:MongoDB-聚合操作--聚合管道--$geoNear

    ①$geoNear 使用“$geoNear”可以得到附近的坐标点. ②范例:准备测试数据

  7. 微信小程序与Vue js数据渲染对比

    //小程序 Page({ data: { items: [] }, onLoad: function(options) { this.setData({ items: [1,2,3] }) } }) ...

  8. Windows 下 Quartus 检测不到 USB-Blaster 终极解决办法

    转自https://blog.csdn.net/acang301/article/details/50471067?locationNum=12 一.Windows无法正常驱动USB-Blaster ...

  9. char类型

    1.JAVA中,char占2字节,16位.可在存放汉字 2.char赋值 char a='a';  //任意单个字符,加单引号. char a='中';//任意单个中文字,加单引号. char a=1 ...

  10. Silverlight中图片显示

    silverlight中显示一个图片有很多的中方法,xaml中的image控件或者自定编写程序来生成image控件. silverlight中显示的图片只能是Bitmap, JPG, PNG(64位颜 ...