gpt4 book ai didi

Javascript - 有没有更有效的方法来创建数组数组? - 提供示例

转载 作者:行者123 更新时间:2023-11-29 16:35:52 27 4
gpt4 key购买 nike

问题:是否有更有效的方法来创建递增数字数组的数组?

我创建了一个函数来生成递增数字数组的数组,这花费了比预期更长的时间,并且我确信有一种更有效的方法来实现此目的(我是 JS 新手)。

示例 1 和 2 的 genArray 函数注意事项:argu1 声明数字范围的开始(例如 0 = 从 0 开始),argu2 声明数字范围的结束(例如 9 = 在 9 处结束),argu3 声明每个数组需要多少个数字(例如 3 = 在数组中生成 3 个数字),argu4 携带临时数组来生成单个数字数组,argu5 通过函数和嵌套函数携带数组的数组。

示例 1:下面的代码纯粹用于创建递增数字数组的数组。我的问题涉及制作此函数的更高效版本。

function genArray(start, finish, quantity, array, allArray = []) {
var collectArray = allArray;

//Cycle through digits from start to finish, e.g. 0-9
for (var i = start; i <= finish; i++) {
var tempArray = [];

//Collect digits for a single array if not first iteration
if (array !== undefined) {
tempArray = tempArray.concat(array);
};

//Add digit to single level array
tempArray.push(i);

//If not highest level, go higher
if (quantity > 1) {
var genArray2 = genArray(start, finish, quantity-1, tempArray, collectArray);
}

//If highest level collect a single array
else if (quantity == 1) {
collectArray.push(tempArray);
}
}
return collectArray;
}

//Call function with arguments
//argu1 declares the start of the number range, argu2 declares the end of the number range, argu3 declares how many numbers are needed in each individual array, argu4 carrays the temp array to generate a single array of numbers, argu4 carrys the array of arrays throught the function and nested functions.
var genArray2 = genArray(0, 9, 3);
console.log(genArray2);

这会生成如下日志:

[ [ 0, 0, 0 ],
[ 0, 0, 1 ],
[ 0, 0, 2 ],
[ 0, 0, 3 ],
[ 0, 0, 4 ],
[ 0, 0, 5 ],
[ 0, 0, 6 ],
[ 0, 0, 7 ],
[ 0, 0, 8 ],
[ 0, 0, 9 ],
[ 0, 1, 0 ],
[ 0, 1, 1 ],
[ 0, 1, 2 ],
[ 0, 1, 3 ],
[ 0, 1, 4 ],
[ 0, 1, 5 ],
[ 0, 1, 6 ],
[ 0, 1, 7 ],
[ 0, 1, 8 ],
[ 0, 1, 9 ],
[ 0, 2, 0 ],
[ 0, 2, 1 ],
[ 0, 2, 2 ],
[ 0, 2, 3 ],
[ 0, 2, 4 ],
[ 0, 2, 5 ],
[ 0, 2, 6 ],
[ 0, 2, 7 ],
[ 0, 2, 8 ],
[ 0, 2, 9 ],
[ 0, 3, 0 ],
[ 0, 3, 1 ],
[ 0, 3, 2 ],
[ 0, 3, 3 ],
[ 0, 3, 4 ],
.... up to [ 9, 9, 9 ]

示例 2:下面是我实际使用的代码,唯一的更改是添加了一个检查,以查看生成的数组是否为升序且每个数字是否唯一,并且仅存储在这两种情况下都正确的那些。提供此内容作为上下文,以防它对某人有用:

//Check if ascending
function ascending(x) {
return x == parseInt(x.toString().split('').sort().join(''));
}

//Check if unique
function unique(x) {
return x.toString().split('').length == [...new Set(x)].length
}

//Create an array of arrays of ascending and unique numbers
function genArray(start, finish, quantity, array, allArray = []) {
var collectArray = allArray;

//Cycle through digits from start to finish, e.g. 0-9
for (var i = start; i <= finish; i++) {
var tempArray = [];

//Collect digits for a single array if not first iteration
if (array !== undefined) {
tempArray = tempArray.concat(array);
};

//Add digit to single level array
tempArray.push(i);

//If not highest level, go higher
if (quantity > 1) {
var genArray2 = genArray(start, finish, quantity-1, tempArray, collectArray);
}

//If highest level collect a single array
else if (quantity == 1 && ascending(tempArray.join('')) && unique(tempArray.join(''))) {
collectArray.push(tempArray);
}
}
return collectArray;
}

//Call function with arguments
var genArray2 = genArray(0, 9, 3);
console.log(genArray2);

这会生成如下日志:

[ [ 0, 1, 2 ],
[ 0, 1, 3 ],
[ 0, 1, 4 ],
[ 0, 1, 5 ],
[ 0, 1, 6 ],
[ 0, 1, 7 ],
[ 0, 1, 8 ],
[ 0, 1, 9 ],
[ 0, 2, 3 ],
[ 0, 2, 4 ],
[ 0, 2, 5 ],
[ 0, 2, 6 ],
[ 0, 2, 7 ],
[ 0, 2, 8 ],
[ 0, 2, 9 ],
[ 0, 3, 4 ],
[ 0, 3, 5 ],
[ 0, 3, 6 ],
[ 0, 3, 7 ],
[ 0, 3, 8 ],
[ 0, 3, 9 ],
[ 0, 4, 5 ],
[ 0, 4, 6 ],
[ 0, 4, 7 ],
[ 0, 4, 8 ],
[ 0, 4, 9 ],
[ 0, 5, 6 ],
[ 0, 5, 7 ],
[ 0, 5, 8 ],
[ 0, 5, 9 ],
[ 0, 6, 7 ],
[ 0, 6, 8 ],
[ 0, 6, 9 ],
[ 0, 7, 8 ],
[ 0, 7, 9 ],
[ 0, 8, 9 ],
[ 1, 2, 3 ],
[ 1, 2, 4 ],
[ 1, 2, 5 ],
[ 1, 2, 6 ],
.... up to [ 7, 8, 9 ]

最佳答案

无需递归,您将能够加快速度。这是一个循环,仅使用先前添加的子数组来计算下一个。它使用将 1 添加到十进制数时所采用的机制:首先递增最右边的数字。如果超出范围(十进制:变为 10),则将其设置回最低数字并递增其左侧的数字,...等等,直到最后更改的数字保持在范围内:

function genArray(start, finish, quantity) {
const current = Array(quantity).fill(start);
const result = [];
for (let i = quantity; i >= 0; null) {
result.push(current.slice());
for (i = quantity; i--; null) {
current[i]++;
if (current[i] <= finish) break;
current[i] = start;
}
}
return result;
}

console.log(genArray(0, 2, 3));

关于Javascript - 有没有更有效的方法来创建数组数组? - 提供示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51345661/

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