Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 具有最大长度参数的置换_Javascript_Algorithm - Fatal编程技术网

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添加到函数中,并在您再次调用它或生成“全局”变量时传递它)这是否回答了您的问题?