gpt4 book ai didi

javascript - Js - 解决挑战 Range Sum Query - Immutable with creating object and its properties

转载 作者:行者123 更新时间:2023-11-29 23:29:57 24 4
gpt4 key购买 nike

我在 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)

我是否应该创建一个包含 mums 和 creats 以及数组的属性 createNew,如果 nums 已经作为数组发送,我为什么要这样做?

最佳答案

what exactly it means is this part:

var obj = Object.create(NumArray).createNew(nums)

他们把它搞砸了,意思只是 var obj = new NumArray(nums)(对于像 JS 这样的函数式语言来说,这仍然是不必要的复杂)。

显然,描述在某些时候被一些无知的人更改了,因为讨论部分中的所有旧 JavaScript 提交(例如 this one)在评论中都有正确的代码。

关于javascript - Js - 解决挑战 Range Sum Query - Immutable with creating object and its properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47698109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com