内容:
Given an integer array nums, find the sum of the elements
class NumArray {
public:
NumArray(vector<int> &nums) {
arr = nums;
}
int sumRange(int i, int j) {
int sum = 0;
while (i <= j)
{
sum += arr[i];
i++;
}
return sum;
}
private:
vector<int> arr;
};between indices i and j (i ≤ j), inclusive.
思路:
类中有一个数组,构造函数把传入的数组赋值给类的数组。
求和时从起始下标开始遍历至终止下标。
本文介绍了一个简单的类实现,用于快速计算整数数组中指定区间的元素之和。通过构造函数初始化数组,然后利用成员函数sumRange按指定范围进行求和。
690

被折叠的 条评论
为什么被折叠?



