所以我現在有這個代碼,並在輸入我有升序我的名字的字母「ahimrsu」。我需要爲所有2170組合顯示正確的「mariush」數字。現在只顯示ahimrsu,ahimrus,ahimsru,ahimsur,ahimurs,ahimusr,ahirmus,ahirmsu ....等。我該怎麼做這個?Java排列alghorithm
<!DOCTYPE HTML>
<html>
<head>
<!--Script Function Start Here-->
<script type="text/javascript">
function perms(data) {
if (!(data instanceof Array)) {
throw new TypeError("input data must be an Array");
}
data = data.slice(); // make a copy
var permutations = [],
stack = [];
function doPerm() {
if (data.length == 0) {
permutations.push(stack.slice());
}
for (var i = 0; i < data.length; i++) {
var x = data.splice(i, 1);
stack.push(x);
doPerm();
stack.pop();
data.splice(i, 0, x);
}
}
doPerm();
return permutations;
}
var input = "ahimrsu".split('');
var result = perms(input);
for (var i = 0; i < result.length; i++) {
result[i] = result[i].join('');
}
console.log(result);
</script>
<!--Header start here-->
</head>
<body>
<!--Script Result-->
<script type="text/javascript">
document.write(result);
</script>
</body>
</html>
你到目前爲止做了什麼? – ToYonos 2014-10-22 08:11:22
我建議你閱讀一般的變化和排列。 – JBA 2014-10-22 08:12:22
這個公式是'factorial(str.length)'。 – dfsq 2014-10-22 08:12:25