Javascript 具有最大长度参数的置换
我正在编写一个函数来创建具有最大长度限制的所有可能排列,数组中的每个项只能使用一次 原始排列代码如下所示:Javascript 具有最大长度参数的置换,javascript,algorithm,Javascript,Algorithm,我正在编写一个函数来创建具有最大长度限制的所有可能排列,数组中的每个项只能使用一次 原始排列代码如下所示: function permutations(list) { // Empty list has one permutation if (list.length == 0) return [[]]; let result = []; for (let i=0; i<list.length; i++) { //
function permutations(list)
{
// Empty list has one permutation
if (list.length == 0)
return [[]];
let result = [];
for (let i=0; i<list.length; i++)
{
// Clone list (kind of)
let copy = Object.create(list);
// Cut one element from list
let head = copy.splice(i, 1);
// Permute rest of list
let rest = permutations(copy);
// Add head to each permutation of rest of list
for (let j=0; j<rest.length; j++)
{
let next = head.concat(rest[j]);
result.push(next);
}
}
return result;
}
函数置换(列表)
{
//空列表有一个排列
如果(list.length==0)
返回[];
让结果=[];
对于(让i=0;i,从极简主义的角度来看,停止递归不是在从列表大小缩小列表时,而是在从列表中提取maxLength元素时
函数置换(列表,最大长度)
{
//空列表有一个排列
如果(maxLength==0)
返回[];
让结果=[];
对于(让i=0;我必须将它作为param添加到函数中,并在您再次调用它或生成“全局”变量时传递它)这是否回答了您的问题?