LeetCode OJ:Range Sum Query 2D - Immutable(区域和2D版本)
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).
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.
前面一个博文的衍生版本,由原来的一维矩阵变成了现在的二维矩阵,没什么区别,还是先建立和的数组,代码如下所示:
class NumMatrix {
public:
NumMatrix(vector<vector<int>> &matrix){
if(!matrix.size() || !matrix[].size())
return;
sum = vector<vector<int>>(matrix.size(), vector<int>(matrix[].size(), ));
for(int i = ; i < matrix.size(); ++i){
for(int j = ; j < matrix[].size(); ++j){
if(i != && j != ){
sum[i][j] = matrix[i][j] + sum[i][j-] + sum[i-][j] - sum[i-][j-];
}else if(i == && j == ){
sum[i][j] = matrix[i][j];
}else if(i == ){
sum[i][j] = matrix[i][j] + sum[i][j-];
}else{
sum[i][j] = matrix[i][j] + sum[i-][j];
}
}
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
if(row1 == && col1 == )
return sum[row2][col2];
else if(row1 == )
return sum[row2][col2] - sum[row2][col1-];
else if(col1 == )
return sum[row2][col2] - sum[row1-][col2];
else
return sum[row2][col2] - sum[row2][col1-] - sum[row1-][col2] + sum[row1-][col1-];
}
private:
vector<vector<int>> sum;
};
LeetCode OJ:Range Sum Query 2D - Immutable(区域和2D版本)的更多相关文章
- [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] 303. Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] 307. Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- LeetCode 308. Range Sum Query 2D - Mutable
原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...
- [LeetCode] 303. Range Sum Query - Immutable (Easy)
303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...
- [Leetcode Week16]Range Sum Query - Mutable
Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...
- 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二维区间求和 - 不变
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 ...
随机推荐
- Use the SVN command-line tool
欢迎关注我的社交账号: 博客园地址: http://www.cnblogs.com/jiangxinnju/p/4781259.html GitHub地址: https://github.com/ji ...
- 关于hashmap 与concurrentHashMap
hashmap是不安全的,要实现安全,可以用Collections里面的synchronizedMap包裹来实现安全,或者用concurrentMap, 注意:hashtable是安全的 从JDK1. ...
- java并发之Lock以及和synchronized区别
从Java5之后,在Java.util.concurrent.locks包下提供了另外一种方式来实现同步访问,那就是Lock. 1.Lock 首先要说明的就是Lock,通过查看Lock的源码可知,Lo ...
- LINUX SHELL 笔记 01: 脚本
root@iZwz:~/labs# vim myfirst root@iZwz:~/labs# cat myfirst #!/bin/bash clear echo "this is my ...
- MAC VNC SSH roo用户开通
第一步:ssh: user@xxx.xxx.xxx 第二步:开启VNC 开启VNC共享桌面: sudo /System/Library/CoreServices/RemoteManagement/A ...
- Java学习第四周学习笔记
20145307<Java程序设计>第4周学习总结 教材学习内容总结 继承与多态 继承 继承作为面向对象的第二大特征,基本上就是避免多个类间重复定义共同行为.即当多个类中存在相同属性和行为 ...
- 20145215《Java程序设计》第二周学习总结
教材内容总结 类型.变量与运算符 *基本类型 整数(short.int.long) 字节(byte) 浮点数(float/double) 字符(char)将一个数字字母或者符号用单引号标识,字符串用双 ...
- # 20145327 《Java程序设计》第七周学习总结
20145327 <Java程序设计>第七周学习总结 教材学习内容总结 只有Lambda表达式,参数的类型必须写出来,如果有目标类型,在编译程序可推断出类型的情况下,可以不写出. GMT时 ...
- Keepalived安装配置入门
准备两台虚拟机,IP如下: A:192.168.1.11 B:192.168.1.12 A为Master,B为BackUp 1.安装 yum install keepalived -y 2.配置 A服 ...
- C# 操作FTP
操作FTP管理类: using System; using System.Collections.Generic; using System.Text; using System.Net; using ...