problemscpp
A collection of my answers to algorithm problems in c++.
Public 成员函数 | Private 属性 | 所有成员列表
leetcode::range_sum_query_mutable::NumArray类 参考

#include <leetcode.h>

Public 成员函数

 NumArray (vector< int > &nums)
 Initializes the object with the integer array nums. 更多...
 
int sumRange (int left, int right) const
 Returns the sum of the elements of nums between indices left and right inclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]). 更多...
 
void update (int index, int val)
 Updates the value of nums[index] to be val. 更多...
 

Private 属性

vector< int > & nums
 
int size
 
vector< int > sum
 

详细描述

在文件 leetcode.h2091 行定义.

构造及析构函数说明

◆ NumArray()

leetcode::range_sum_query_mutable::NumArray::NumArray ( vector< int > &  nums)
explicit

Initializes the object with the integer array nums.

在文件 leetcode.cpp5657 行定义.

5658 : nums(nums) {
5659 const int n = nums.size();
5660 size = sqrt(n);
5661 sum.resize((n + size - 1) / size);// n/size 向上取整
5662 for(int i = 0; i < n; i++) {
5663 sum[i / size] += nums[i];
5664 }
5665 }

引用了 nums, size , 以及 sum.

成员函数说明

◆ sumRange()

int leetcode::range_sum_query_mutable::NumArray::sumRange ( int  left,
int  right 
) const

Returns the sum of the elements of nums between indices left and right inclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]).

返回
The sum of the elements of nums between indices left and right inclusive

在文件 leetcode.cpp5672 行定义.

5672 {
5673 const int b1 = left / size;
5674 const int i1 = left % size;
5675 const int b2 = right / size;
5676 const int i2 = right % size;
5677 if(b1 == b2) {
5678 // 区间 [left, right] 在同一块中
5679 return accumulate(nums.begin() + b1 * size + i1, nums.begin() + b1 * size + i2 + 1, 0);
5680 }
5681 const int sum1 = accumulate(nums.begin() + b1 * size + i1, nums.begin() + b1 * size + size, 0);
5682 const int sum2 = accumulate(nums.begin() + b2 * size, nums.begin() + b2 * size + i2 + 1, 0);
5683 const int sum3 = accumulate(sum.begin() + b1 + 1, sum.begin() + b2, 0);
5684 return sum1 + sum2 + sum3;
5685 }

引用了 acwing::acwing1929::left, nums, acwing::acwing1929::right, size , 以及 sum.

◆ update()

void leetcode::range_sum_query_mutable::NumArray::update ( int  index,
int  val 
)

Updates the value of nums[index] to be val.

在文件 leetcode.cpp5667 行定义.

5667 {
5668 sum[index / size] += val - nums[index];
5669 nums[index] = val;
5670 }

引用了 nums, size , 以及 sum.

类成员变量说明

◆ nums

vector<int>& leetcode::range_sum_query_mutable::NumArray::nums
private

在文件 leetcode.h2094 行定义.

被这些函数引用 NumArray(), sumRange() , 以及 update().

◆ size

int leetcode::range_sum_query_mutable::NumArray::size
private

在文件 leetcode.h2093 行定义.

被这些函数引用 NumArray(), sumRange() , 以及 update().

◆ sum

vector<int> leetcode::range_sum_query_mutable::NumArray::sum
private

在文件 leetcode.h2092 行定义.

被这些函数引用 NumArray(), sumRange() , 以及 update().


该类的文档由以下文件生成: