javascript – 获取所有可能的唯一排列

有一个带有一些符号的小数组,如[‘^’,’^’,’>’,’>’,”,’<','<'],我怎样才能获得所有不同的排列?我知道类似的问题已被提出(并且已经有一些很好的答案),如:
> Shuffle an array as many as possible
> Permutations in JavaScript?

但是它们没有呈现出独特的结果.我怎样才能有效地获得每个可能的结果一次?

解决方法:

对于小数组,您可以使用其中一个引用的算法,将每个排列映射到一个字符串,并将整个数组抛出到Set中以丢弃重复项.就像是:

let a = ['^','^','>','>','+','<','<'];
let ps = permutations(a);  // return value should be array of arrays.
let qs = ps.map(p => p.join(""));
let s = new Set(qs);

对于具有<的数组,这应该可以正常工作10个符号. 否则,请参阅herehere,了解可以转换为JavaScript的各种方法.

一种流行的方法是Pandita algorithm,其使用连续规则列举词典顺序中的排列,有效地仅生成“唯一”排列. herehere给出了这种方法的简短说明.这是一个JavaScript(ES6)实现:

function swap(a, i, j) {
    const t = a[i];
    a[i] = a[j];
    a[j] = t;
}

function reverseSuffix(a, start) {
    if (start === 0) {
        a.reverse();
    }
    else {
        let left = start;
        let right = a.length - 1;

        while (left < right)
            swap(a, left++, right--);
    }
}

function nextPermutation(a) {
    // 1. find the largest index `i` such that a[i] < a[i + 1].
    // 2. find the largest `j` (> i) such that a[i] < a[j].
    // 3. swap a[i] with a[j].
    // 4. reverse the suffix of `a` starting at index (i + 1).
    //
    // For a more intuitive description of this algorithm, see:
    //   https://www.nayuki.io/page/next-lexicographical-permutation-algorithm
    const reversedIndices = [...Array(a.length).keys()].reverse();

    // Step #1; (note: `.slice(1)` maybe not necessary in JS?)
    const i = reversedIndices.slice(1).find(i => a[i] < a[i + 1]);

    if (i === undefined) {
        a.reverse();
        return false;
    } 

    // Steps #2-4
    const j = reversedIndices.find(j => a[i] < a[j]);
    swap(a, i, j);
    reverseSuffix(a, i + 1);
    return true;
}

function* uniquePermutations(a) {
    const b = a.slice().sort();

    do {
        yield b.slice();
    } while (nextPermutation(b));
}

let a = ['^','^','>','>','+','<','<'];
let ps = Array.from(uniquePermutations(a));
let qs = ps.map(p => p.join(""));

console.log(ps.length);
console.log(new Set(qs).size);

nextPermutation函数将数组就地转换为词典后继,或者如果数组已经是字典最大值,则将字典转换为词典最小值.在第一种情况下,它返回true,否则返回false.这允许您循环遍历从最小(已排序)数组开始直到nextPermutation翻转并返回false的所有排列.

上一篇:java – 字符数组的每个组合


下一篇:【微信小程序】认识微信小程序