summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-09-23 08:37:05 +0800
committercrupest <crupest@outlook.com>2020-09-23 08:37:05 +0800
commitf49db3c2b70fb23767e40460edc7967b920a95f6 (patch)
tree84b7f65774f5f3a8f9399df9002a7b044e1ab1a1 /cpp
parent23925b650c3c2d50ef33142c9de9b31399b3eca8 (diff)
downloadsolutions-f49db3c2b70fb23767e40460edc7967b920a95f6.tar.gz
solutions-f49db3c2b70fb23767e40460edc7967b920a95f6.tar.bz2
solutions-f49db3c2b70fb23767e40460edc7967b920a95f6.zip
Add problem 303 .
Diffstat (limited to 'cpp')
-rw-r--r--cpp/303.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/cpp/303.cpp b/cpp/303.cpp
new file mode 100644
index 0000000..74aea3c
--- /dev/null
+++ b/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];
+ }
+};