-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDp303.java
More file actions
54 lines (52 loc) · 1.55 KB
/
Dp303.java
File metadata and controls
54 lines (52 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package dynamicprogramming;
public class Dp303 {
class NumArray {
int[] sum;
public NumArray(int[] nums) {
sum = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
if (i == 0) {
sum[i] = nums[i];
} else {
sum[i] = sum[i - 1] + nums[i];
}
}
}
public int sumRange(int i, int j) {
if (i == 0) {
return sum[j];
} else {
return sum[j] - sum[i-1];
}
}
}
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/
/**
* Runtime: 52 ms, faster than 59.70% of Java online submissions for Range Sum Query - Immutable.
* Memory Usage: 39.9 MB, less than 100.00% of Java online submissions for Range Sum Query - Immutable.
*/
// class NumArray{
// int[] sum;
// public NumArray(int[] nums) {
// sum = new int[nums.length];
// for (int i = 0; i < nums.length; i++) {
// if (i == 0) {
// sum[i] = nums[i];
// } else {
// sum[i] = sum[i - 1] + nums[i];
// }
// }
// }
// public int sumRange(int i, int j) {
// if (i == 0) {
// return sum[j];
// } else {
// return sum[j] - sum[i-1];
// }
// }
// }
}