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. VS2005中SetUnhandledExceptionFilter函数应用

    很多软件通过设置自己的异常捕获函数,捕获未处理的异常,生成报告或者日志(例如生成mini-dump文件),达到Release版本下追踪Bug的目的.但是,到了VS2005(即VC8),Microsof ...

  2. linux samba.tar.gz安装和配置

    安装步骤: 1. tar -xzvf samba-3.5.10.tar.gz2. cd samba-3.5.103. cd source34. ./autogen.sh  如果出现:./autogen ...

  3. xcode 树形管理 cocos2d-x的资源

    把资源以目录的形式加入xcode中, 同时, 在加入时, 选择"Create Folder References for any  added folders", 而不是默认的&q ...

  4. [itint5]判断是否为二叉搜索树

    http://www.itint5.com/oj/#25 这题在leetcode上是用中序遍历来做的,但是这里由于有相等的情况,即左子树小于等于根,这样中序遍历无法完全判定.可以用递归来做,用递归给出 ...

  5. CF339

    C. Xenia and Weights 有1...10k的砝码,在天枰上,左右轮流放置砝码,要求之后左右轮流比另一侧重量要大,要求相邻两次砝码不能相同. 解题报告给出(i,j,k)表示balance ...

  6. Linux下的动态连接库及其实现机制

    Linux与Windows的动态连接库概念相似,但是实现机制不同.它引入了GOT表和PLT表的概念,综合使用了多种重定位项,实现了"浮动代码",达到了更好的共享性能.本文对这些技术 ...

  7. WCF 传输的序列化

    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService”.[ServiceContract]public interface IService{ [O ...

  8. node.js 异步式I/O 与事件驱动

    Node.js 最大的特点就是异步式 I/O(或者非阻塞 I/O)与事件紧密结合的编程模式.这种模式与传统的同步式 I/O 线性的编程思路有很大的不同,因为控制流很大程度上要靠事件和回调函数来组织,一 ...

  9. php修改排序,上移下移

    php修改排序,上移下移 /**    $UpDown //移动方向,up或down    $table //表名    $id //当前移动的ID    $id_col //ID字段的名称    $ ...

  10. 【HDOJ】4029 Distinct Sub-matrix

    思路是枚举矩阵列数,然后将字符矩阵转换成字符串,通过字符数组求不同子串数目.最后,减去不成立的情况.使用特殊字符分割可能的组合. /* 4029 */ #include <iostream> ...