-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path[303]区域和检索 - 数组不可变.java
More file actions
48 lines (39 loc) · 1.07 KB
/
Copy path[303]区域和检索 - 数组不可变.java
File metadata and controls
48 lines (39 loc) · 1.07 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
//leetcode submit region begin(Prohibit modification and deletion)
//class NumArray {
//
// public int[] nums ;
//
// public NumArray(int[] nums) {
// this.nums = nums;
// }
//
// public int sumRange(int left, int right) {
// //解法一:
// int res = 0;
// for (int i = left; i <= right; i++) {
// res = nums[i]+res;
// }
//
// return res;
// }
//}
//解法二:先预处理,然后再进行调用
class NumArray {
public int[] preNums ;
public NumArray(int[] nums) {
preNums = new int[nums.length+1];
for (int i = 1; i < preNums.length; i++) {
preNums[i] = preNums[i-1]+nums[i-1];
}
}
public int sumRange(int left, int right) {
return preNums[right+1]-preNums[left];
}
//解法二:先预处理,然后再进行调用
}
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(left,right);
*/
//leetcode submit region end(Prohibit modification and deletion)