[尝试使用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不随香草JS一起提供,您必须使用npm进行安装
npm install --save collections
或者只是使用内置的数组方法来模拟双端队列操作。