1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <vector> using std::vector; class NumArray { private: vector<int> prefix_sums; public: NumArray(vector<int> &nums) { prefix_sums.push_back(0); int sum = 0; for (auto num : nums) { sum += num; prefix_sums.push_back(sum); } } int sumRange(int i, int j) { return prefix_sums[j + 1] - prefix_sums[i]; } };