We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b2ab339 commit 04527c8Copy full SHA for 04527c8
303_Range_Sum_Query_Immutable.cc
@@ -0,0 +1,27 @@
1
+class NumArray {
2
+public:
3
+ NumArray(vector<int> &nums) {
4
+ if(nums.size()==0) return;
5
+ int tmp=0;
6
+ for(int i=0;i<nums.size();++i){
7
+ tmp+=nums[i];
8
+ sums.push_back(tmp);
9
+ }
10
11
+
12
+ int sumRange(int i, int j) {
13
+ if(i>j) return 0;
14
+ if(i==0) return sums[j];
15
+ else return sums[j]-sums[i-1];
16
17
18
19
+private:
20
+ vector<int> sums;
21
+};
22
23
24
+// Your NumArray object will be instantiated and called as such:
25
+// NumArray numArray(nums);
26
+// numArray.sumRange(0, 1);
27
+// numArray.sumRange(1, 2);
0 commit comments