Js - 解决挑战 Range Sum Query - Immutable with creating object and its properties
Js - solving challenge Range Sum Query - Immutable with creating object and its properties
我正在查看 leetcode 站点上的问题,发现一个我不知道如何解决的问题。他们是这样解释的:
Given an integer array nums, find the sum of the elements between
indices i and j (i ≤ j), inclusive.
Example: Given nums = [-2, 0, 3, -5, 2, -1]
sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 Note:
You may assume that the array does not change. There are many calls to
sumRange function.
这是代码:
/**
* @param {number[]} nums
*/
var NumArray = function(nums) {
this.nums = nums;
};
/**
* @param {number} i
* @param {number} j
* @return {number}
*/
NumArray.prototype.sumRange = function(i, j) {
let sum = 0;
for (i; i <= j; i++) {
sum += this[i];
}
return sum;
};
/**
* Your NumArray object will be instantiated and called as such:
* var obj = Object.create(NumArray).createNew(nums)
* var param_1 = obj.sumRange(i,j)
*/
我不知道该怎么做,或者这部分到底是什么意思:
var obj = Object.create(NumArray).createNew(nums)
我是否应该创建一个 属性 createNew 来接受 mums 和 creats 以及数组,如果 nums 已经作为数组发送,我为什么要这样做?
what exactly it means is this part:
var obj = Object.create(NumArray).createNew(nums)
他们把它搞砸了,意思很简单 var obj = new NumArray(nums)
(对于像 JS 这样的函数式语言来说,这仍然是不必要的复杂)。
显然,描述在某些时候被一些无知的人更改了,因为讨论部分(例如 this one)中所有旧的 JavaScript 提交在评论中都有正确的代码。
我正在查看 leetcode 站点上的问题,发现一个我不知道如何解决的问题。他们是这样解释的:
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example: Given nums = [-2, 0, 3, -5, 2, -1]
sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 Note: You may assume that the array does not change. There are many calls to sumRange function.
这是代码:
/**
* @param {number[]} nums
*/
var NumArray = function(nums) {
this.nums = nums;
};
/**
* @param {number} i
* @param {number} j
* @return {number}
*/
NumArray.prototype.sumRange = function(i, j) {
let sum = 0;
for (i; i <= j; i++) {
sum += this[i];
}
return sum;
};
/**
* Your NumArray object will be instantiated and called as such:
* var obj = Object.create(NumArray).createNew(nums)
* var param_1 = obj.sumRange(i,j)
*/
我不知道该怎么做,或者这部分到底是什么意思:
var obj = Object.create(NumArray).createNew(nums)
我是否应该创建一个 属性 createNew 来接受 mums 和 creats 以及数组,如果 nums 已经作为数组发送,我为什么要这样做?
what exactly it means is this part:
var obj = Object.create(NumArray).createNew(nums)
他们把它搞砸了,意思很简单 var obj = new NumArray(nums)
(对于像 JS 这样的函数式语言来说,这仍然是不必要的复杂)。
显然,描述在某些时候被一些无知的人更改了,因为讨论部分(例如 this one)中所有旧的 JavaScript 提交在评论中都有正确的代码。