Skip to content

Commit e7b43fb

Browse files
Create 0303-range-sum-query-immutable.java
1 parent c49180f commit e7b43fb

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Diff for: java/0303-range-sum-query-immutable.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class NumArray {
2+
3+
public int[] arr;
4+
public NumArray(int[] nums) {
5+
arr=nums;
6+
for(int i=1;i<arr.length;i++){
7+
arr[i]+=arr[i-1];
8+
}
9+
}
10+
11+
public int sumRange(int left, int right) {
12+
if(left==0){
13+
return arr[right];
14+
}
15+
return arr[right]-arr[left-1];
16+
}
17+
}
18+
19+
/**
20+
* Your NumArray object will be instantiated and called as such:
21+
* NumArray obj = new NumArray(nums);
22+
* int param_1 = obj.sumRange(left,right);
23+
*/

0 commit comments

Comments
 (0)