获得所有可能的独特排列

时间:2016-10-26 14:01:50

标签: javascript algorithm permutation

如果数组包含['^','^','>','>','+','<','<']之类的符号,我如何获得所有不同的排列?我知道类似的问题已被提出(并且已经有一些很好的答案),如:

但是,它们不会显示唯一结果。我怎样才能有效只获得一次可能的结果?

2 个答案:

答案 0 :(得分:3)

对于小数组,您可以使用其中一个引用的算法,将每个排列映射到一个字符串,并将整个数组抛出到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,它使用连续规则列举词典顺序中的排列,实际上只生成&#34; unique&#34;排列。对此方法的简短解释是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

答案 1 :(得分:0)

好的唯一结果问题显然是效率杀手,因为每次创建新的排列时都必须检查结果列表。至于算法,它将以与其他排列算法基本相同的方式工作,但是删除重复标准将涉及更多检查。如果阵列的大小很小,效率不应该是一个大问题。如果已找到的值不添加到数组,只需循环遍历应答数组。加速此检查过程的一种方法是确定对答案数组进行排序的方法。例如,^总是在*之前出现(然后你不必每次检查整个数组。还有其他方法可以加快速度,但在一天结束时它仍然是一个计算成本非常高的要求。由于你的数组很小,所以除非你打算做这个排列ALOT

,否则根本不重要
相关问题