-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumArray.java
60 lines (53 loc) · 1.84 KB
/
NumArray.java
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
55
56
57
58
59
60
package algorithms;
/**
* 给定一个整数数组 nums,求出数组从索引i到j(i≤j)范围内元素的总和,包含i、j两点。
* <p>
* 实现 NumArray 类:
* <p>
* NumArray(int[] nums) 使用数组 nums 初始化对象
* int sumRange(int i, int j) 返回数组 nums 从索引i到j(i≤j)范围内元素的总和,包含i、j两点(也就是 sum(nums[i], nums[i + 1], ... , nums[j]))
* <p>
* <p>
* 示例:
* <p>
* 输入:
* ["NumArray", "sumRange", "sumRange", "sumRange"]
* [[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
* 输出:
* [null, 1, -1, -3]
* <p>
* 解释:
* NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
* numArray.sumRange(0, 2); // return 1 ((-2) + 0 + 3)
* numArray.sumRange(2, 5); // return -1 (3 + (-5) + 2 + (-1))
* numArray.sumRange(0, 5); // return -3 ((-2) + 0 + 3 + (-5) + 2 + (-1))
*/
/**
* 解题思路
* 转换 -> nums[i] = nums[0]+nums[1]+...+nums[i]
* 则 nums[i,j] = nums[j]-nums[i] = nums[i+1]+...+nums[j]
*/
class NumArray {
int[] nums;
public NumArray(int[] nums) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
nums[i] = sum;
}
this.nums = nums;
}
public int sumRange(int left, int right) {
if (left == 0) {
return nums[right];
} else {
return nums[right] - nums[left - 1];
}
}
public static void main(String[] args) {
NumArray numArray = new NumArray(new int[]{-2, 0, 3, -5, 2, -1});
System.out.println(numArray.sumRange(0, 2));// return 1 ((-2) + 0 + 3)
System.out.println(numArray.sumRange(2, 5));// return -1 (3 + (-5) + 2 + (-1))
System.out.println(numArray.sumRange(0, 5));// return -3 ((-2) + 0 + 3 + (-5) + 2 + (-1))
}
}