Leetcode 303 Solution
This article provides solution to leetcode question 303 (range-sum-query-immutable)
Access this page by simply typing in "lcs 303" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/range-sum-query-immutable
Solution
class NumArray {
vector<int> sums;
public:
NumArray(vector<int> &nums) {
sums.resize(nums.size());
int sum = 0;
for (int i = 0; i < nums.size(); i++)
{
sum += nums[i];
sums[i] = sum;
}
}
int sumRange(int i, int j) {
if (i == 0)
return sums[j];
else
return sums[j] - sums[i - 1];
}
};
// Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);