[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) ,右 ...
随机推荐
- tp5系统核心设计提供了一些可能会需要的钩子(位置)
钩子 描述 参数 app_init 应用初始化标签位 无 app_begin 应用开始标签位 当前调度信息 module_init 模块初始化标签位 当前请求对象实例 action_begin ...
- sql_id VS hash_value
有没有发现,v$session,v$sql,v$sqlarea,v$sqltext,v$sql_shared_cursor等试图连接的时候经常会用到hash_value,sql_id,但是他们2个之间 ...
- (转)ASP.NET MVC3 Razor视图引擎-基础语法
转自:http://kb.cnblogs.com/page/96883/ I:ASP.NET MVC3在Visual Studio 2010中的变化 在VS2010中新建一个MVC3项目可以看出与以往 ...
- Beta阶段第三篇Scrum冲刺博客-Day2
1.站立式会议 提供当天站立式会议照片一张 2.每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 昨天已完成的工作. 张晨晨:熟悉代码 郭琪容:了解复习模块需要的部分知识 ...
- noip第27课作业
1. 繁忙的都市 [问题描述] 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个 ...
- Lombok自定义annotation扩展含Intellij插件
Lombok简介 Lombok(https://projectlombok.org/) 提供了以注解的形式为java对象增加属性和方法,这使得原来冗长的java源文件变的简洁(不需要再使用ide去生 ...
- Codeforces822 C. Hacker, pack your bags!
C. Hacker, pack your bags! time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- POJ2566 Bound Found 2017-05-25 20:05 32人阅读 评论(0) 收藏
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4056 Accepted: 1249 Spe ...
- 《如何阅读it技术书》课堂笔记——51cto
对一些书的看法: “21天精通JAVA之类”的书,好好理解精通二字,哪里有这么快就能学的会. 吐槽新人: Oop理论,别写出来的都是面向过程式. 桌面乱七八糟. 对新人分享一些经验: 阅读时自我提神的 ...
- hdu 4937 base进制只含3456的base数
http://acm.hdu.edu.cn/showproblem.php?pid=4937 给定一个数n,若这个数在base进制下全由3,4,5,6组成的话,则称base为n的幸运进制,给定n,求有 ...