题目

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. Manacher算法,最长回文串

    给你10000长度字符串,然你求最长回文字串,输出长度,暴力算法肯定超时 #include <iostream> #include <string> #include < ...

  2. 学以致用五----centos7+python3.6.2+django2.1.1

    目的,在python 3.6的基础上搭建 django 2.x 一.使用pip安装django ,但是使用pip命令的时候报错,解决方法,做软连接 ln -s /usr/local/python/bi ...

  3. 模板引擎(smarty)知识点总结四

    /*   smarty 引入对象 */ require_once 'libs/Smarty.class.php';  require 'MySmarty.class.php';  $msma = ne ...

  4. 移动端 - Android客户端性能测试常见指标

    rom版本的性能测试 一般关注功耗(不过 rom 版本的功耗测试跟应用的功耗测试会有所差异,当然只是用例设计方面的差异,工具仍然采用安捷伦电源仪进行) 应用的性能测试 包括很多测试项,如启动时间.内存 ...

  5. 广搜 迷宫(zznu 1962)

    http://acm.zznu.edu.cn/problem.php?id=1962 题目描述 在很多 RPG (Role-playing Games) 游戏中,迷宫往往是非常复杂的游戏环节.通常来说 ...

  6. codeblocks+SDCC开发51单片机

    说到51,大部分人都是用的是KEIL开发环境,但是KEIL是商业软件,我们一般人都用的是破解版的,如果用于商业就会收到法律诉讼.然而有一款很好的编译器专为51内核而存在.SDCC最大的有点就是开源免费 ...

  7. RMQ算法区间最值

    问题类型:是多次询问一个大区间里子区间的最值问题 dp + 位运算的思想处理 rmax[i][j]表示从i开始到i + 2^j - 1的区间里的最大值dp[i][j] ==== (i,i + 2^j ...

  8. 二分图学习——基础dfs判断二分图

    #include <iostream> #include <cstdio> #include <string.h> #include <vector> ...

  9. bootstrap3相关文档

    ,每列分配多列 <divclass="container"> <div class="row"> <div class=" ...

  10. 代码面试集锦 1 - Uber

    Given an array of integers, return a new array such that each element at index i of the new array is ...