Skip to content

Commit 9ff3039

Browse files
authored
303.NumArray-一维前缀和
1 parent 6ed0eb0 commit 9ff3039

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

NumArray.java

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

0 commit comments

Comments
 (0)