找不到模块 "collections/deque"

Cannot find module "collections/deque"

尝试使用 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

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