文章

javascript 递归函数实现排列组合

递归函数案例

javascript 递归函数实现排列、组合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{
    function combinations(x,m) {
        const elements = [...x];
        const n = x.length;
        if(m == 0)
            return [[]];
        else if (m > n)
            return [];
        else if(m==n)
            return [elements];
        const prefix = elements.shift(); // 第一个元素
        const with_first = []
        for(let item of combinations(elements,m-1))
            with_first.push([prefix].concat(item));
        const without_first = []
        for(let item of combinations(elements,m))
            without_first.push(item);
        return with_first.concat(without_first)
    }

    function permutations(x,m) {
        const elements = [...x];
        const n = x.length;
        if (m==0) 
            return [[]];
        else if (m>n)
            return [];
        const perms = []
        for(let i=0;i<x.length;i++) {
            const current = x[i];
            const rest = [...x]
            rest.splice(i,1) // 去除第 i 号元素
            for(let item of permutations(rest,m-1))
                perms.push([current].concat(item));
        }
        return perms;
    }
    const combs = combinations("ABCD",3);
    console.log('组合 ==> %o',combs)

    const perms = permutations("ABCD",3);
    console.log('排列 ==> %o',perms)
}
本文由作者按照 CC BY 4.0 进行授权