File tree Expand file tree Collapse file tree 4 files changed +102
-18
lines changed
solution/0300-0399/0303.Range Sum Query - Immutable Expand file tree Collapse file tree 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>
3333<!-- 这里可写当前语言的特殊实现逻辑 -->
3434
3535``` python
36+ class NumArray :
3637
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)
3752```
3853
3954### ** Java**
4055
4156<!-- 这里可写当前语言的特殊实现逻辑 -->
4257
4358``` 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+ */
4581```
4682
4783### ** ...**
Original file line number Diff line number Diff line change @@ -43,13 +43,49 @@ sumRange(0, 5) -> -3
4343### ** Python3**
4444
4545``` python
46+ class NumArray :
4647
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)
4762```
4863
4964### ** Java**
5065
5166``` 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+ */
5389```
5490
5591### ** ...**
Original file line number Diff line number Diff line change 11class NumArray {
22
3- private int [] nums ;
4- private int [] sums ;
3+ private int [] sums ;
54
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+ }
2016}
2117
2218/**
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