https://leetcode.com/problems/range-sum-query-immutable/

class NumArray {

public: vector<int> vec;
public:
NumArray(vector<int> &nums) {
if(nums.empty()) return;
else {
vec.push_back(nums[]);
for(int i=;i<nums.size();++i) vec.push_back(vec[i-] + nums[i]);
}
} int sumRange(int i, int j) {
if(i< || j>=vec.size()) return ;
if(i==) return vec[j];
else return vec[j] - vec[i-];
}
}; // Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);

https://leetcode.com/problems/range-sum-query-2d-immutable/

class NumMatrix {
public:
vector<vector<int>> vec;
NumMatrix(vector<vector<int>> &matrix) {
if(matrix.size() == || matrix[].size() == ) return;
vec.resize(matrix.size());
for(int i=;i<vec.size();++i) vec[i].resize(matrix[].size()); vec[][] = matrix[][];
for(int col=;col<vec[].size();++col) vec[][col] = matrix[][col] + vec[][col-]; for(int row=;row<vec.size();++row) {
for(int col=;col<vec[row].size();++col) {
if(col == ) vec[row][col] = matrix[row][col];
else vec[row][col] = matrix[row][col] + vec[row][col-];
}
} for(int row=;row<vec.size();++row) {
for(int col=;col<vec[row].size();++col) {
vec[row][col] += vec[row-][col];
}
} for(int i=;i<vec.size();++i) {
for(int j=;j<vec[i].size();++j) cout<<vec[i][j]<<" ";
cout<<endl;
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
if(row1< || row2>=vec.size() || col1< || col2>=vec[].size()) return ; int leftTop = ;
if(row1 == || col1 == ) leftTop = ;
else if(row1 && col1) leftTop = vec[row1-][col1-]; int ret1 = ;
if(row1 == ) ret1 = vec[row2][col2];
else if(row1 > ) ret1 = vec[row2][col2] - vec[row1-][col2]; int ret2 = ;
if(col1 == ) ret2 = ret1;
else if(col1 > ) ret2 = ret1 - vec[row2][col1-] + leftTop; return ret2;
}
}; // Your NumMatrix object will be instantiated and called as such:
// NumMatrix numMatrix(matrix);
// numMatrix.sumRegion(0, 1, 2, 3);
// numMatrix.sumRegion(1, 2, 3, 4);

leetcode@ [303/304] Range Sum Query - Immutable / Range Sum Query 2D - Immutable的更多相关文章

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

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

  3. 【LeetCode】304. Range Sum Query 2D - Immutable 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 预先求和 相似题目 参考资料 日期 题目地址:htt ...

  4. 【刷题-LeetCode】304. Range Sum Query 2D - Immutable

    Range Sum Query 2D - Immutable Given a 2D matrix matrix, find the sum of the elements inside the rec ...

  5. Leetcode 303 Range Sum Query - Immutable

    题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...

  6. LeetCode 303. Range Sum Query - Immutable (C++)

    题目: Given an integer array nums, find the sum of the elements between indices iand j (i ≤ j), inclus ...

  7. LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)

    翻译 给定一个整型数组nums,找出在索引i到j(i小于等于j)之间(包含i和j)的全部元素之和. 比如: 给定nums = [-2,0,3,-5,2,-1] sumRange(0, 2) -> ...

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

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

随机推荐

  1. 每次都觉得很神奇的JS

    匿名,函数对象... var staff = [ {name: 'abruzzi', age: 24}, {name: 'bajmine', age: 26}, {name: 'chris', age ...

  2. java使用redis

    1.redis和memecache这种缓存框架,都分为服务器端和客户端. 我们在项目中使用,相当于客户端,要引入客户端使用redis的jar包 首先你需要下载驱动包,下载 jedis.jar,确保下载 ...

  3. 一张图让你看懂锻压、CNC、压铸、冲压四类工艺!

    (锻压+CNC+阳极.CNC+阳极.压铸+阳极.冲压+阳极手机外壳比较) 上图为一张雷达图,该图比较直观形象地描述了4大手机外壳工艺在6个维度(加工成本.CNC用量.加工周期.成品率.可设计性.外观质 ...

  4. POJ3259——Wormholes(Bellman-Ford+SPFA)

    Wormholes DescriptionWhile exploring his many farms, Farmer John has discovered a number of amazing ...

  5. Go语言Revel框架 环境搭建

    1.首先参考连个链接 http://blog.csdn.net/creak_phone/article/details/12620969 http://www.geek521.com/?p=616 2 ...

  6. Oracle DBA常用SQL

    监控SQL 1.监控事例的等待: select event,sum(decode(wait_time,0,0,1)) prev, sum(decode(wait_time,0,1,0)) curr,c ...

  7. Hibernate映射之实体映射<转载>

    实体类与数据库之间存在某种映射关系,Hibernate依据这种映射关系完成数据的存取,因此映射关系的配置在Hibernate中是最关键的.Hibernate支持xml配置文件与@注解配置两种方式.xm ...

  8. bzoj3774

    这算是最小割中比较难的吧 看到选取显然最小割 看到上下左右四个点我感觉肯定和染色相关 注意每个点的收益获得条件是[或],因此我们考虑拆点i', i,分别表示通过四周控制和控制本身的代价 连边s--&g ...

  9. ↗☻【编写可维护的JavaScript #BOOK#】第9章 将配置数据从代码中分离出来

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  10. 【转】[MTK软件原创] [SELinux] 如何设置确认selinux模式

    原文网址:http://bbs.16rd.com/thread-54766-1-1.html [Description] linux SELinux 分成Enforce 以及 Permissive 两 ...