-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumArray.js
More file actions
40 lines (38 loc) · 994 Bytes
/
Copy pathNumArray.js
File metadata and controls
40 lines (38 loc) · 994 Bytes
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
/**
* @filename NumArray.js
* @author 56
* @description https://leetcode-cn.com/problems/range-sum-query-immutable/
*/
/**
* @param {number[]} nums
*/
var NumArray = function (nums) {
this.nums = nums
const sums = [0]
this.sums = sums
for (let i = 1, len = nums.length + 1; i < len; i++) {
sums[i] = sums[i - 1] + nums[i - 1]
}
}
/**
* 1. 看了题解. 用前缀和来优化
* @param {number} i
* @param {number} j
* @return {number}
*/
NumArray.prototype.sumRange = function (i, j) {
return this.sums[j + 1] - this.sums[i]
// const nums = this.nums
// if (!Array.isArray(nums)) return 0
// const len = this.nums.length
// if (len === 0 || i > len) return 0
// let sum = 0
// for (let k = i; k <= j && k < len; k++) sum += nums[k]
// return sum
}
/**
* Your NumArray object will be instantiated and called as such:
* var obj = new NumArray(nums)
* var param_1 = obj.sumRange(i,j)
*/
console.info(new NumArray([1, 2, 3, 4]).sumRange(1, 2))