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. Nagios Apache报Internal Server Error错误的解决方法

    今天配置Nagios的时候遇到了一些麻烦,前面的步骤都一切顺利,nagios运行后,可以看到nagios的主页,但点击左边的菜单时总是提示Internal Server Error错误.错误如下: v ...

  2. SPRING IN ACTION 第4版笔记-第九章Securing web applications-008-使用非关系型数据库时如何验证用户(自定义UserService)

    一. 1.定义接口 Suppose that you need to authenticate against users in a non-relational database suchas Mo ...

  3. knowledge about apache

    http://wenku.baidu.com/link?url=6O51BQJdtFRFWDGszKfN3aK7IY92QTCpuc7miBhRLazXvxL5gXb18B_TqIdi3EruX1o_ ...

  4. cookie分析

    浏览器cookie分析 简单点说就是数据存储,通过JavaScript将要保存的数据保存在客户端浏览器,下次打开网页时调用保存的cookie.浏览器中cookie保存的形式:user=aa; pwd= ...

  5. 制作计算器的代码(C#)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. git源码中的Makefile

    https://github.com/chucklu/GitStudy   这链接里面的第一次提交 [chucklu@localhost GitStudy]$ cat Makefile CFLAGS= ...

  7. Android 内存管理分析(四)

    尊重原创作者,转载请注明出处: http://blog.csdn.net/gemmem/article/details/8920039 最近在网上看了不少Android内存管理方面的博文,但是文章大多 ...

  8. poj3225 线段树区间操作 (见鬼)

    细节处理实在太重要了. #include<cstdio> #include<cstring> #define MT 65533*4 #define Maxn MT*4 int ...

  9. 三个流行MySQL分支的对比

    MySQL是历史上最受欢迎的免费开源程序之一.它是成千上万个网站的数据库骨干,并且可以将它(和Linux)作为过去10年里Internet呈指数级增长的一个有力证明. 那么,如果MySQL真的这么重要 ...

  10. [swustoj 856] Huge Tree

    Huge Tree(0856) 问题描述 There are N trees in a forest. At first, each tree contains only one node as it ...