File tree 5 files changed +81
-0
lines changed
algorithms/RangeSumQueryImmutable
5 files changed +81
-0
lines changed Original file line number Diff line number Diff line change @@ -57,6 +57,7 @@ All solutions will be accepted!
57
57
| 811| [ Subdomain Visit Count] ( https://leetcode-cn.com/problems/subdomain-visit-count/description/ ) | [ java/py/js] ( ./algorithms/SubdomainVisitCount ) | Easy|
58
58
| 637| [ Average Of Levels In Binary Tree] ( https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/description/ ) | [ java/py/js] ( ./algorithms/AverageOfLevelsInBinaryTree ) | Easy|
59
59
| 118| [ Pascals Triangle] ( https://leetcode-cn.com/problems/pascals-triangle/description/ ) | [ java/py/js] ( ./algorithms/PascalsTriangle ) | Easy|
60
+ | 303| [ Range Sum Query Immutable] ( https://leetcode-cn.com/problems/range-sum-query-immutable/description/ ) | [ java/py/js] ( ./algorithms/RangeSumQueryImmutable ) | Easy|
60
61
61
62
# Database
62
63
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Range Sum Query Immutable
2
+ Use advance calculation to solve this problem
Original file line number Diff line number Diff line change
1
+ class NumArray {
2
+ Map <Integer , Integer > sumMap = new HashMap <Integer , Integer >();
3
+
4
+ public NumArray (int [] nums ) {
5
+ for (int i = 0 ; i < nums .length ; i ++) {
6
+ sumMap .put (i , i == 0 ? nums [i ] : sumMap .get (i - 1 ) + nums [i ]);
7
+ }
8
+ }
9
+
10
+ public int sumRange (int i , int j ) {
11
+ return i == 0 ? sumMap .get (j ) : sumMap .get (j ) - sumMap .get (i - 1 );
12
+ }
13
+ }
14
+
15
+ /**
16
+ * Your NumArray object will be instantiated and called as such:
17
+ * NumArray obj = new NumArray(nums);
18
+ * int param_1 = obj.sumRange(i,j);
19
+ */
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ */
4
+ var NumArray = function ( nums ) {
5
+ this . sumMap = { }
6
+ nums . forEach ( ( num , index ) => {
7
+ if ( index === 0 ) {
8
+ this . sumMap [ index ] = num
9
+ } else {
10
+ this . sumMap [ index ] = this . sumMap [ index - 1 ] + num
11
+ }
12
+ } )
13
+ } ;
14
+
15
+ /**
16
+ * @param {number } i
17
+ * @param {number } j
18
+ * @return {number }
19
+ */
20
+ NumArray . prototype . sumRange = function ( i , j ) {
21
+ if ( i === 0 ) {
22
+ return this . sumMap [ j ]
23
+ } else {
24
+ return this . sumMap [ j ] - this . sumMap [ i - 1 ]
25
+ }
26
+ } ;
27
+
28
+ /**
29
+ * Your NumArray object will be instantiated and called as such:
30
+ * var obj = Object.create(NumArray).createNew(nums)
31
+ * var param_1 = obj.sumRange(i,j)
32
+ */
Original file line number Diff line number Diff line change
1
+ class NumArray (object ):
2
+
3
+ def __init__ (self , nums ):
4
+ """
5
+ :type nums: List[int]
6
+ """
7
+ self .sum_map = {}
8
+ for i in range (0 , len (nums )):
9
+ if i == 0 :
10
+ self .sum_map [i ] = nums [i ]
11
+ else :
12
+ self .sum_map [i ] = self .sum_map [i - 1 ] + nums [i ]
13
+
14
+ def sumRange (self , i , j ):
15
+ """
16
+ :type i: int
17
+ :type j: int
18
+ :rtype: int
19
+ """
20
+ if i == 0 :
21
+ return self .sum_map [j ]
22
+ return self .sum_map [j ] - self .sum_map [i - 1 ]
23
+
24
+
25
+ # Your NumArray object will be instantiated and called as such:
26
+ # obj = NumArray(nums)
27
+ # param_1 = obj.sumRange(i,j)
You can’t perform that action at this time.
0 commit comments