diff options
Diffstat (limited to 'store/works/solutions/leetcode/cpp/303.cpp')
-rw-r--r-- | store/works/solutions/leetcode/cpp/303.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/store/works/solutions/leetcode/cpp/303.cpp b/store/works/solutions/leetcode/cpp/303.cpp new file mode 100644 index 0000000..06c4ad1 --- /dev/null +++ b/store/works/solutions/leetcode/cpp/303.cpp @@ -0,0 +1,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];
+ }
+};
|