内容:
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.
思路:
类中有一个数组,构造函数把传入的数组赋值给类的数组。
求和时从起始下标开始遍历至终止下标。