-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.js
58 lines (53 loc) · 1.21 KB
/
res.js
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
/**
* Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
*
* The update(i, val) function modifies nums by updating the element at index i to val.
*
* Example:
* Given nums = [1, 3, 5]
* sumRange(0, 2) -> 9
* update(1, 2)
* sumRange(0, 2) -> 8
*
* Note:
* The array is only modifiable by the update function.
*
* You may assume the number of calls to update and sumRange function is distributed evenly.
*
* res.js
* @authors Joe Jiang (hijiangtao@gmail.com)
* @date 2017-02-28 20:10:00
* @version $Id$
*/
/**
* @param {number[]} nums
*/
var NumArray = function(nums) {
this.nums = nums;
};
/**
* @param {number} i
* @param {number} val
* @return {void}
*/
NumArray.prototype.update = function(i, val) {
this.nums[i] = val;
};
/**
* @param {number} i
* @param {number} j
* @return {number}
*/
NumArray.prototype.sumRange = function(i, j) {
let sum = 0;
for (let ind=i; ind<=j; ind++) {
sum += this.nums[ind];
}
return sum;
};
/**
* Your NumArray object will be instantiated and called as such:
* var obj = Object.create(NumArray).createNew(nums)
* obj.update(i,val)
* var param_2 = obj.sumRange(i,j)
*/