我想这并不难。这只是标准的排列方式。您需要使用一点递归:
function permute(size) {
var range = getRange(size);
var result = [];
getSubPerms('', range, result);
return result;
};
function getRange(size) {
var range = [];
for (var i = 0; i < size; i++) {
range.push(i + 1);
}
return range;
}
function getSubPerms(perm, range, result) {
for (var i = 0; i < range.length; i++) {
var perm2 = perm + range[i];
if (perm2.length == range.length) {
result.push(perm2);
} else {
getSubPerms(perm2, range, result);
}
}
}
var foo = permute(4); //an array of all of your results.
alert(foo.length); //256
However, if you're only interested in the length of that, without having to generate the results, it would simply be Math.pow(size, size)
to get the length of your results.