File tree 4 files changed +102
-18
lines changed
solution/0300-0399/0303.Range Sum Query - Immutable
4 files changed +102
-18
lines changed Original file line number Diff line number Diff line change @@ -33,15 +33,51 @@ sumRange(0, 5) -> -3</pre>
33
33
<!-- 这里可写当前语言的特殊实现逻辑 -->
34
34
35
35
``` python
36
+ class NumArray :
36
37
38
+ def __init__ (self , nums : List[int ]):
39
+ n = len (nums)
40
+ self .sums = [0 ] * (n + 1 )
41
+ for i in range (n):
42
+ self .sums[i + 1 ] = nums[i] + self .sums[i]
43
+
44
+
45
+ def sumRange (self , i : int , j : int ) -> int :
46
+ return self .sums[j + 1 ] - self .sums[i]
47
+
48
+
49
+ # Your NumArray object will be instantiated and called as such:
50
+ # obj = NumArray(nums)
51
+ # param_1 = obj.sumRange(i,j)
37
52
```
38
53
39
54
### ** Java**
40
55
41
56
<!-- 这里可写当前语言的特殊实现逻辑 -->
42
57
43
58
``` java
44
-
59
+ class NumArray {
60
+
61
+ private int [] sums;
62
+
63
+ public NumArray (int [] nums ) {
64
+ int n = nums. length;
65
+ sums = new int [n + 1 ];
66
+ for (int i = 0 ; i < n; ++ i) {
67
+ sums[i + 1 ] = nums[i] + sums[i];
68
+ }
69
+ }
70
+
71
+ public int sumRange (int i , int j ) {
72
+ return sums[j + 1 ] - sums[i];
73
+ }
74
+ }
75
+
76
+ /**
77
+ * Your NumArray object will be instantiated and called as such:
78
+ * NumArray obj = new NumArray(nums);
79
+ * int param_1 = obj.sumRange(i,j);
80
+ */
45
81
```
46
82
47
83
### ** ...**
Original file line number Diff line number Diff line change @@ -43,13 +43,49 @@ sumRange(0, 5) -> -3
43
43
### ** Python3**
44
44
45
45
``` python
46
+ class NumArray :
46
47
48
+ def __init__ (self , nums : List[int ]):
49
+ n = len (nums)
50
+ self .sums = [0 ] * (n + 1 )
51
+ for i in range (n):
52
+ self .sums[i + 1 ] = nums[i] + self .sums[i]
53
+
54
+
55
+ def sumRange (self , i : int , j : int ) -> int :
56
+ return self .sums[j + 1 ] - self .sums[i]
57
+
58
+
59
+ # Your NumArray object will be instantiated and called as such:
60
+ # obj = NumArray(nums)
61
+ # param_1 = obj.sumRange(i,j)
47
62
```
48
63
49
64
### ** Java**
50
65
51
66
``` java
52
-
67
+ class NumArray {
68
+
69
+ private int [] sums;
70
+
71
+ public NumArray (int [] nums ) {
72
+ int n = nums. length;
73
+ sums = new int [n + 1 ];
74
+ for (int i = 0 ; i < n; ++ i) {
75
+ sums[i + 1 ] = nums[i] + sums[i];
76
+ }
77
+ }
78
+
79
+ public int sumRange (int i , int j ) {
80
+ return sums[j + 1 ] - sums[i];
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Your NumArray object will be instantiated and called as such:
86
+ * NumArray obj = new NumArray(nums);
87
+ * int param_1 = obj.sumRange(i,j);
88
+ */
53
89
```
54
90
55
91
### ** ...**
Original file line number Diff line number Diff line change 1
1
class NumArray {
2
2
3
- private int [] nums ;
4
- private int [] sums ;
3
+ private int [] sums ;
5
4
6
- public NumArray (int [] tmp ) {
7
- this .nums = Arrays .copyOf (tmp , tmp .length );
8
- sums = new int [nums .length + 1 ];
9
- for (int i = 0 ; i < nums .length ; i ++) {
10
- sums [i + 1 ] += nums [i ] + sums [i ];
11
- }
12
- }
13
-
14
- public int sumRange (int i , int j ) {
15
- if (i < 0 || j > nums .length || i > j ) {
16
- return 0 ;
17
- }
18
- return sums [j + 1 ] - sums [i ];
19
- }
5
+ public NumArray (int [] nums ) {
6
+ int n = nums .length ;
7
+ sums = new int [n + 1 ];
8
+ for (int i = 0 ; i < n ; ++i ) {
9
+ sums [i + 1 ] = nums [i ] + sums [i ];
10
+ }
11
+ }
12
+
13
+ public int sumRange (int i , int j ) {
14
+ return sums [j + 1 ] - sums [i ];
15
+ }
20
16
}
21
17
22
18
/**
Original file line number Diff line number Diff line change
1
+ class NumArray :
2
+
3
+ def __init__ (self , nums : List [int ]):
4
+ n = len (nums )
5
+ self .sums = [0 ] * (n + 1 )
6
+ for i in range (n ):
7
+ self .sums [i + 1 ] = nums [i ] + self .sums [i ]
8
+
9
+
10
+ def sumRange (self , i : int , j : int ) -> int :
11
+ return self .sums [j + 1 ] - self .sums [i ]
12
+
13
+
14
+ # Your NumArray object will be instantiated and called as such:
15
+ # obj = NumArray(nums)
16
+ # param_1 = obj.sumRange(i,j)
You can’t perform that action at this time.
0 commit comments