gpt4 book ai didi

javascript - 找不到模块 "collections/deque"

转载 作者:行者123 更新时间:2023-12-02 22:41:18 25 4
gpt4 key购买 nike

尝试使用 Deque 数据结构来回答一个编程问题,以查找乘积小于目标的所有子数组。

如上所述,我想使用 Deque 数据结构。我查看了用法,认为我做对了,但是使用了

const Deque = require("collections/deque");

但是,我收到错误:

Cannot find module "collections/deque"

如果有人能看到我缺少的内容,请告诉我。

这是代码。

const Deque = require("collections/deque");

function find_subarrays(arr, target) {
let result = [],
product = 1,
left = 0;
for (right = 0; right < arr.length; right++) {
product *= arr[right];
while (product >= target && left < arr.length) {
product /= arr[left];
left += 1;
}
const tempList = new Deque();
for (let i = right; i > left - 1; i--) {
tempList.unshift(arr[i]);
result.push(tempList.toArray());
}
}
return result;
}

最佳答案

问题是 collections.js 没有附带 vanilla JS,你必须使用 npm 安装它

npm install --save collections

或者只是使用内置的数组方法来模拟双端队列操作。

关于javascript - 找不到模块 "collections/deque",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58578304/

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