Skip to content

Commit 04527c8

Browse files
author
cpppy
authored
Create 303_Range_Sum_Query_Immutable.cc
1 parent b2ab339 commit 04527c8

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

303_Range_Sum_Query_Immutable.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)