-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation.html
More file actions
42 lines (34 loc) · 1.11 KB
/
permutation.html
File metadata and controls
42 lines (34 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>全排列</title>
</head>
<body>
<script>
var permutation = function (string) {
var arr = string.split('')
function permute(arr) {
if (arr.length === 1) {
return arr;
}
if (arr.length === 2) {
return [arr[0] + arr[1], arr[1] + arr[0]];
}
let result = []
for (let index = 0; index < arr.length; index++) {
let pivot = arr.splice(index, 1)[0];
result = result.concat(permute(arr).map(item => pivot + item))
arr.splice(index, 0, pivot)
}
return [...new Set(result)];
}
return permute(arr)
};
let res = permutation('ABCDEFGHA'); // 超过10位内存可能溢出
console.log(res);
</script>
</body>
</html>