## 303. Range Sum Query - Immutable 原

N3verL4nd

Given an integer arraynums, find the sum of the elements between indicesiandj(ij), inclusive.

Example:

``````Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3``````

Note:

1. You may assume that the array does not change.

1. There are many calls tosumRangefunction.

``````#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
using namespace std;

class NumArray {
public:
NumArray(vector<int> &nums)
{
n = nums.size();
for (int i = 1; i <= n; i++)
{
num[i] = 0;
}
for (vector<int>::size_type i = 0; i < nums.size(); i++)
{
Update(i + 1, nums[i]);
}
}

int sumRange(int i, int j)
{
return Sum(j + 1) - Sum(i);
}
private:
int LowBit(int x)
{
return x & (-x);
}
int Sum(int end)
{
int sum = 0;
while (end > 0)
{
sum += num[end];
end -= LowBit(end);
}
return sum;
}
void Update(int pos, int value)
{
while (pos <= n)
{
num[pos] += value;
pos += LowBit(pos);
}
}
int num[1000000];
int n;
};

int main()
{
vector<int>vec{ -2, 0, 3, -5, 2, -1 };
NumArray ss(vec);
cout << ss.sumRange(0, 5);
return 0;
}``````

num数组开的比较大

### N3verL4nd

LeetCode：Range Sum Query 2D -Immutable-矩阵指定区域内的元素和

1、题目名称 Range Sum Query 2D（矩阵指定区域内的元素和） 2、题目地址 https://leetcode.com/problemset/algorithms/ 3、题目内容 英文：Given a 2D matrix matrix, find the sum of the...

2015/11/15
1K
0
Leetcode 303. Range Sum Query - Immutable

SnailTyan
02/18
0
0
[LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

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). The above rectangl......

2017/12/15
0
0
[LeetCode] Count of Range Sum 区间和计数

Given an integer array , return the number of range sums that lie in inclusive. Range sum is defined as the sum of the elements in between indices and ( ≤ ), inclusive. Note: ......

2017/12/15
0
0
LeetCode：Range Sum Query - Immutable - 数组指定区间内的元素和

1、题目名称 Range Sum Query（数组指定区间内的元素和） 2、题目地址 https://leetcode.com/problems/range-sum-query-immutable/ 3、题目内容 英文：Given an integer array nums, find t...

2015/11/10
3.3K
0

PostgreSQL参数search_path影响及作用

search_path稍微熟悉PG就会用到，用法这里就不必讲，本篇主要讲它在程序里怎样处理。 1、GUC参数定义 这是个 config_string 参数 {{"search_path", PGC_USERSET, CLIENT_CONN_STATEMENT,...

58分钟前
8
0
Qt程序各个平台打包发布及安装程序大全

shzwork

4
0
springboot2.0 maven打包分离lib，resources

springboot将工程打包成jar包后，会出现获取classpath下的文件出现测试环境正常而生产环境文件找不到的问题，这是因为 1、在调试过程中，文件是真实存在于磁盘的某个目录。此时通过获取文件路...

22
0
BootStrap

wytao1995

10
0

18
0