English 中文(简体)
调查结果 Java形形形形形形形形状的全汇( product产品)
原标题:Finding All Combinations (Cartesian product) of JavaScript array values

我怎么能够把 Java印成形长数阵列的数值组合起来?

我要说的是,有 N印阵列的N号,例如:

var first = [ a ,  b ,  c ,  d ];
var second = [ e ];
var third =  [ f ,  g ,  h ,  i ,  j ];

(例如,三个阵列,但其为这一问题而设的阵列数目。)

我也想拿出所有价值观的组合,以生产这些价值观。

aef
aeg
aeh
aei
aej
bef
beg
....
dej

EDIT:此处用我接受的回答作为基础。

var allArrays = [[ a ,  b ], [ c ,  z ], [ d ,  e ,  f ]];

 function allPossibleCases(arr) {
  if (arr.length === 0) {
    return [];
  } 
  else if (arr.length ===1){
    return arr[0];
  }
  else {
    var result = [];
    var allCasesOfRest = allPossibleCases(arr.slice(1));  // recur with the rest of array
    for (var c in allCasesOfRest) {
      for (var i = 0; i < arr[0].length; i++) {
        result.push(arr[0][i] + allCasesOfRest[c]);
      }
    }
    return result;
  }

}
var results = allPossibleCases(allArrays);
 //outputs ["acd", "bcd", "azd", "bzd", "ace", "bce", "aze", "bze", "acf", "bcf", "azf", "bzf"]
最佳回答
问题回答

你们不需要再行,或大量nes,甚至要创造/储存各种记忆。

由于每一阵列的长度是每一阵列的产物(请上<代码>numPerms),你可创设一个功能<代码>getPermutation(n),在索引代码<0><>>/代码>和之间实现独一无二的变动。 Perms - 1,根据n计算其需要检索其特性的指数。

How is this done? If you think of creating permutations on arrays each containing: [0, 1, 2, ... 9] it s very simple... the 245th permutation (n=245) is "245", rather intuitively, or:

arrayHundreds[Math.floor(n / 100) % 10]
+ arrayTens[Math.floor(n / 10) % 10]
+ arrayOnes[Math.floor(n / 1) % 10]

贵问题的复杂性是,阵列的规模各不相同。 我们可以通过将<代码>n/100、n/10等替换为其他分母。 为此,我们可以很容易预先确定一系列分歧。 在上述例子中,100名主持人等于arrayTens.length*阵列。 因此,我们可以计算出一个阵列的分歧点,作为剩余阵列长度的产物。 最后一个阵列总是有1个分歧。 而且,我们不是用10dding,而是用目前阵列的长度 mo笑。

例典如下:

var allArrays = [first, second, third, ...];

// Pre-calculate divisors
var divisors = [];
for (var i = allArrays.length - 1; i >= 0; i--) {
   divisors[i] = divisors[i + 1] ? divisors[i + 1] * allArrays[i + 1].length : 1;
}

function getPermutation(n) {
   var result = "", curArray;

   for (var i = 0; i < allArrays.length; i++) {
      curArray = allArrays[i];
      result += curArray[Math.floor(n / divisors[i]) % curArray.length];
   }

   return result;
}

回答对我来说太困难。 因此,我的解决办法是:

var allArrays = new Array([ a ,  b ], [ c ,  z ], [ d ,  e ,  f ]);

function getPermutation(array, prefix) {
  prefix = prefix ||   ;
  if (!array.length) {
    return prefix;
  }

  var result = array[0].reduce(function(result, value) {
    return result.concat(getPermutation(array.slice(1), prefix + value));
  }, []);
  return result;
}

console.log(getPermutation(allArrays));

您可以采取单一方针,产生一种tes产品。

result = items.reduce(
    (a, b) => a.reduce(
        (r, v) => r.concat(b.map(w => [].concat(v, w))),
        []
    )
);

var items = [[ a ,  b ,  c ,  d ], [ e ], [ f ,  g ,  h ,  i ,  j ]],
    result = items.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []));
	
console.log(result.map(a => a.join(   )));
.as-console-wrapper { max-height: 100% !important; top: 0; }

直接接收Arrays的电子电解答:

function *combinations(arrOfArr) {
  let [head, ...tail] = arrOfArr
  let remainder = tail.length ? combinations(tail) : [[]];
  for (let r of remainder) for (let h of head) yield [h, ...r];
}

希望能节省时间。

你们可以使用典型的背道:

function cartesianProductConcatenate(arr) {
  var data = new Array(arr.length);
  return (function* recursive(pos) {
    if(pos === arr.length) yield data.join(  );
    else for(var i=0; i<arr[pos].length; ++i) {
      data[pos] = arr[pos][i];
      yield* recursive(pos+1);
    }
  })(0);
}

我利用发电机职能,避免同时分配所有成果,但如果你希望你能够这样做的话。

[...cartesianProductConcatenate([[ a ,  b ], [ c ,  z ], [ d ,  e ,  f ]])];
// ["acd","ace","acf","azd","aze","azf","bcd","bce","bcf","bzd","bze","bzf"]

www.un.org/Depts/DGACM/index_spanish.htm 找到《指南》的最佳途径

const arr1= [  a ,  b ,  c ,  d  ];
const arr2= [  1 ,  2 ,  3  ];
const arr3= [  x ,  y , ];

const all = [arr1, arr2, arr3];

const output = all.reduce((acc, cu) => { 
    let ret = [];
      acc.map(obj => {
        cu.map(obj_1 => {
          ret.push(obj +  -  + obj_1) 
        });
      });
      return ret;
   })

console.log(output);

如果你重新寻找能够处理任何项目类型的两个层面阵列的流动兼容功能,你可使用以下功能。

const getUniqueCombinations = <T>(items : Array<Array<T>>, prepend : Array<T> = []) : Array<Array<T>> => {
    if(!items || items.length === 0) return [prepend];

    let out = [];

    for(let i = 0; i < items[0].length; i++){
        out = [...out, ...getUniqueCombinations(items.slice(1), [...prepend, items[0][i]])];
    }

    return out;
}

a. 业务视觉化:

http://www.hchr.org。

[
    [Obj1, Obj2, Obj3],
    [Obj4, Obj5],
    [Obj6, Obj7]
]

<>>>>>

[
    [Obj1, Obj4, Obj6 ],
    [Obj1, Obj4, Obj7 ],
    [Obj1, Obj5, Obj6 ],
    [Obj1, Obj5, Obj7 ],
    [Obj2, Obj4, Obj6 ],
    [Obj2, Obj4, Obj7 ],
    [Obj2, Obj5, Obj6 ],
    [Obj2, Obj5, Obj7 ],
    [Obj3, Obj4, Obj6 ],
    [Obj3, Obj4, Obj7 ],
    [Obj3, Obj5, Obj6 ],
    [Obj3, Obj5, Obj7 ]
]

您可创建2D阵列和reduce。 然后使用https://developer.mozilla.org/en-US/docs/Web/Java/Reference/Global_Objects/Array/flatMap” rel=“nofollow noretinger”>flatMap,在加固阵列和现有阵列中形成阵容组合,并加固。

const data = [ [ a ,  b ,  c ,  d ], [ e ], [ f ,  g ,  h ,  i ,  j ] ]

const output = data.reduce((acc, cur) => acc.flatMap(c => cur.map(n => c + n)) )

console.log(output)

2021 version of David Tang s great answer
Also inspired with Neil Mountford s answer

const getAllCombinations = (arraysToCombine) => {
  const divisors = [];
  let permsCount = 1;
  for (let i = arraysToCombine.length - 1; i >= 0; i--) {
      divisors[i] = divisors[i + 1] ? divisors[i + 1] * arraysToCombine[i + 1].length : 1;
      permsCount *= (arraysToCombine[i].length || 1);
  }

  const getCombination = (n, arrays, divisors) => arrays.reduce((acc, arr, i) => {
      acc.push(arr[Math.floor(n / divisors[i]) % arr.length]);
      return acc;
  }, []);

  const combinations = [];
  for (let i = 0; i < permsCount; i++) {
      combinations.push(getCombination(i, arraysToCombine, divisors));
  }
  return combinations;
};

console.log(getAllCombinations([[ a ,  b ], [ c ,  z ], [ d ,  e ,  f ]]));

基准:https://jsbench.me/gdkmxhm36d/1

此处采用从上述两种答复中改编的版本,按照《任择议定书》规定的顺序得出结果,并标明回返,而不是阵列:

function *cartesianProduct(...arrays) {
  if (!arrays.length) yield [];
  else {
    const [tail, ...head] = arrays.reverse();
    const beginning = cartesianProduct(...head.reverse());
    for (let b of beginning) for (let t of tail) yield b + t;
  }
}

const first = [ a ,  b ,  c ,  d ];
const second = [ e ];
const third =  [ f ,  g ,  h ,  i ,  j ];
console.log([...cartesianProduct(first, second, third)])

You could use this function too:

const result = (arrayOfArrays) => arrayOfArrays.reduce((t, i) => { let ac = []; for (const ti of t) { for (const ii of i) { ac.push(ti +  /  + ii) } } return ac })

result([[ a ,  b ,  c ,  d ], [ e ], [ f ,  g ,  h ,  i ,  j ]])
// which will output [  a/e/f ,  a/e/g ,  a/e/h , a/e/i , a/e/j , b/e/f , b/e/g , b/e/h , b/e/i , b/e/j , c/e/f , c/e/g , c/e/h , c/e/i , c/e/j , d/e/f , d/e/g , d/e/h , d/e/i , d/e/j ]

当然,你可以删除<代码>+/>,在<代码>ac.push(ti + ii)上,消除最后结果中的斜坡。 您可以取代<条码>(......),代之以各项功能(在<条码>上填上相应的半分辨率/代码>),而不论哪一种功能更适合你们。

a. 不重复的阵列办法:

const combinations = [[ 1 ,  2 ,  3 ], [ 4 ,  5 ,  6 ], [ 7 ,  8 ]];
let outputCombinations = combinations[0]

combinations.slice(1).forEach(row => {
  outputCombinations = outputCombinations.reduce((acc, existing) =>
    acc.concat(row.map(item => existing + item))
  , []);
});

console.log(outputCombinations);
let arr1 = [`a`, `b`, `c`];
let arr2 = [`p`, `q`, `r`];
let arr3 = [`x`, `y`, `z`];
let result = [];

arr1.forEach(e1 => {
    arr2.forEach(e2 => {
        arr3.forEach(e3 => {
            result[result.length] = e1 + e2 + e3;
        });
    });
});


console.log(result);
/*
output:
[
   apx ,  apy ,  apz ,  aqx ,
   aqy ,  aqz ,  arx ,  ary ,
   arz ,  bpx ,  bpy ,  bpz ,
   bqx ,  bqy ,  bqz ,  brx ,
   bry ,  brz ,  cpx ,  cpy ,
   cpz ,  cqx ,  cqy ,  cqz ,
   crx ,  cry ,  crz 
]
*/

a. 不重复处理的解决办法,还包括通过补贴收回单一组合的功能:

function getCombination(data, i) {
    return data.map(group => {
        let choice = group[i % group.length]
        i = (i / group.length) | 0;
        return choice;
    });
}

function* combinations(data) {
    let count = data.reduce((sum, {length}) => sum * length, 1);
    for (let i = 0; i < count; i++) {
        yield getCombination(data, i);
    }
}

let data = [[ a ,  b ,  c ,  d ], [ e ], [ f ,  g ,  h ,  i ,  j ]];

for (let combination of combinations(data)) {
    console.log(...combination);
}




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签