还需要一个数组的所有可能组合,包括组合的反向。
例如:
var b = ['a1','b1','a','b'];
需要组合为:
a1,b1,a,b
a1b1,a1a,a1b, b1a1,b1a,b1b, ......,
a1b1a,a1b1b,a1ab1,a1bb1,........,
a1b1ab,a1b1ba.....bab1a1
所有64个组合(如果数组有4个元素)。 我在Java中使用ArrayList和Collection API找到了解决方案,但是现在我需要一个纯JavaScript ES5解决方案。
我尝试了以下方法,但只提供了较少的组合。
function getCombinations(chars) {
var result = [];
var f = function (prefix, chars) {
for (var i = 0; i < chars.length; i++) {
result.push(prefix + chars[i]);
f(prefix + chars[i], chars.slice(i + 1));
}
}
f('', chars);
return result;
}
答案 0 :(得分:1)
稍微简单的递归将解决此问题。检查一下:
function getCombinations(chars) {
let combinations = [];
chars.forEach((char, i) => {
let word = '';
buildWord(word + char, [i], chars, combinations)
});
return combinations;
}
function buildWord(word, usedIndexes, chars, combinations) {
combinations.push(word);
chars.forEach((char, i) => {
if (usedIndexes.indexOf(i) === -1) {
let newUsedIndexesArray = Array.from(usedIndexes);
newUsedIndexesArray.push(i);
buildWord(word + char, newUsedIndexesArray, chars, combinations)
}
});
}
console.log('Total: ' + getCombinations(['a1', 'b1', 'a', 'b']).length)
console.log(getCombinations(['a1', 'b1', 'a', 'b']))
答案 1 :(得分:0)
让我们把您的要求说出来:对于每个开始元素,请附加其余元素所有组合的所有排列。
function f(A, comb=[], result=[comb]){
for (var i=0; i<A.length; i++)
result = result.concat(f(A.slice(0,i).concat(A.slice(i+1)), comb.concat(A[i])));
return result;
}
console.log(JSON.stringify(f(['a', 'b', 'c', 'd'])));
答案 2 :(得分:0)
下面是一种迭代方法。我们从排列长度1到长度(给定chars
中的字符数),然后为每个长度生成所有可能的组合。
我们维护set
以避免重复,并isValidPermutation()
检查给定的chars
集合中是否可以进行组合以避免无效组合。
function getCombinations(chars) {
if (chars === undefined || chars === null || chars.length === 0) return [];
var final_result = [];
var temp_result_1 = chars.slice();
var set = {};
/* for initial set of elements */
for (var i = 0; i < temp_result_1.length; ++i) {
if (set[temp_result_1[i]] === undefined) {
set[temp_result_1[i]] = true;
final_result.push(temp_result_1[i]);
}
}
/* go from 2 to length(since length 1 is captured above) to get all permutations of combinations */
for (var len = 2; len <= chars.length; ++len) {
var temp_result_2 = [];
for (var i = 0; i < chars.length; ++i) {
for (var j = 0; j < temp_result_1.length; ++j) {
var current_permutation = chars[i] + "," + temp_result_1[j];
if (set[current_permutation] === undefined && isValidPermutation(current_permutation, chars)) {
temp_result_2.push(current_permutation);
set[current_permutation] = true;
}
}
}
temp_result_1 = temp_result_2;
final_result = final_result.concat(temp_result_1);
}
return final_result.map((each) => each.split(",").join(""));
}
/* to check if actually a combination is true and possible from current set of chars */
function isValidPermutation(current_permutation, chars) {
var hash = {};
current_permutation = current_permutation.split(",");
for (var i = 0; i < chars.length; ++i) {
if (hash[chars[i]] === undefined) hash[chars[i]] = 0;
hash[chars[i]]++;
}
for (var i = 0; i < current_permutation.length; ++i) {
hash[current_permutation[i]]--;
if (hash[current_permutation[i]] < 0) return false;
}
return true;
}
var b = ['a1', 'b1', 'a', 'b'];
console.log(getCombinations(b));
由于您需要ECMAScript 5解决方案,因此更改了最后一行
return final_result.map((each) => each.split(",").join(""));
到
for(var i=0;i<final_result.length;++i){
final_result[i] = final_result[i].split(",").join("");
}
return final_result;