多个数组元素的组合 [英] Combinations of elements of multiple arrays

查看:110
本文介绍了多个数组元素的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种类型的数据

  var arr = [
[A,AA] ,
[B],
[C,CC,CCC]
];

我想得到每个数组中所有元素的组合。例如

  AB 
ABC
AB CC
AB CCC
AC
A CC
A CCC
...
AA B CCC

注意单词的顺序是相同的,这样不应该是 BAC 的组合之一。



<我尝试了几个逻辑,但无法得到我想要的东西。我可以获得所有单词的所有排列和组合,但这不是我想要的。



请建议

解决方案

您基本上想要在多个列表中进行置换:

 函数permute(输入)
{
var out = [];

(函数permute_r(输入,当前){
if(input.length === 0){
out.push(current);
return;
}

var next = input.slice(1);

for(var i = 0,n = input [0] .length; i!= n ; ++ i){
permute_r(next,current.concat([input [0] [i]]));
}
}(input,[]));

退出;
}

permute(arr);


I have this type of data

var arr = [
    ["A", "AA"],
    ["B"],
    ["C", "CC", "CCC"]
];

I want to get combinations of all the elements within each array. for e.g.

A B
A B C
A B CC
A B CCC
A C
A CC
A CCC
...
AA B CCC

Note the sequence of the words are same, like this should not be one of the combination B A C.

I tried a couple of logics but can't get what I am looking for. I can obtain all permutations and combinations of all the words, but that's not what I am looking for.

Please suggest

解决方案

You basically want to permute across multiple lists:

function permute(input)
{
  var out = [];

  (function permute_r(input, current) {
    if (input.length === 0) {
      out.push(current);
      return;
    }

    var next = input.slice(1);

    for (var i = 0, n = input[0].length; i != n; ++i) {
      permute_r(next, current.concat([input[0][i]]));
    }
  }(input, []));

  return out;
}

permute(arr);

这篇关于多个数组元素的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆