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. PHP程序员函数注释规格

    <?php/*** @name 名字* @abstract 申明变量/类/方法* @access 指明这个变量.类.函数/方法的存取权限* @author 函数作者的名字和邮箱地址* @cate ...

  2. spoj 39

    DP  完全背包问题 的   d[i] = min(d[i], d[i-w]+p)   d[i]表示当总重量为i时可装的最小价值 #include <cstdio> #include &l ...

  3. Python/Ruby/Go/Node 之四国大战

    Python Flask vs Ruby Sinatra vs Go Martini vs Node Express 本文授权转载自 zybuluo 博客. 题外话一: 最近一段时间,Cloud In ...

  4. express中ejs模板引擎

    1.在 app.js 中通过以下两个语句设置了 引擎类型 和页面模板的位置: app.set('views', __dirname + '/views'); app.set('view engine' ...

  5. chardet安装

    1.下载 chardet-2.2.1.tar.gz (md5)   https://pypi.python.org/pypi/chardet#downloads 2.解压至C:\Python27\Li ...

  6. BindingFlags说明

    为了获取返回值,必须指定 BindingFlags.Instance 或 BindingFlags.Static. 指定 BindingFlags.Public 可在搜索中包含公共成员. 指定 Bin ...

  7. linux查看历史命令history

    在linux下,我们有可能需要查看最近执行过的命令(历史命令),我们可以进行如下操作: # 显示使用过的所有历史命令 $ history # 显示最近使用的5个命令 $ history 5 我们可以通 ...

  8. 【BZOJ 3343 】 分块

    3343: 教主的魔法 Description 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1. ...

  9. NEERC 2014, Eastern subregional contest

    最近做的一场比赛,把自己负责过的题目记一下好了. Problem B URAL 2013 Neither shaken nor stirred 题意:一个有向图,每个结点一个非负值,可以转移到其他结点 ...

  10. Linux 套接字编程中的 5 个隐患

    http://www.ibm.com/developerworks/cn/linux/l-sockpit/ 在 4.2 BSD UNIX® 操作系统中首次引入,Sockets API 现在是任何操作系 ...