-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path二维数组组合算法.html
120 lines (113 loc) · 3.74 KB
/
二维数组组合算法.html
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function combination(originalData) {
var length = originalData.length;
if (length < 2) {
return originalData[0];
}
var firstArrayLength = originalData[0].length;
var secondArrayLength = originalData[1].length;
var result = [];
for (var firstIndex = 0; firstIndex < firstArrayLength; firstIndex++) {
for (var secondIndex = 0; secondIndex < secondArrayLength; secondIndex++) {
result[result.length] = originalData[0][firstIndex] + ',' + originalData[1][secondIndex];
}
}
var tmpResult = [];
for (var i = 2; i < originalData.length; i++) {
tmpResult[i - 1] = originalData[i];
}
tmpResult[0] = result;
return combination(tmpResult);
}
var array = [
['红', '黄', '蓝'],
['S', 'L', 'M'],
['男款', '女款']
];
var items = combination(array);
for (var i = 0; i < items.length; i++) {
document.writeln(items[i]);
document.writeln('<br>');
}
// 方法二:
var arrays = [
["a0", "a1", "a2", "a3"],
["b0", "b1", "b2", "b3"],
["c0", "c1", "c2", "c3"],
["d0", "d1", "d2", "d3"]
];
var array = getArrayByArrays(arrays);
function getArrayByArrays(arrays) {
var arr = [""];
for (var i = 0; i < arrays.length; i++) {
arr = getValuesByArray(arr, arrays[i]);
}
return arr;
}
function getValuesByArray(arr1, arr2) {
var arr = [];
for (var i = 0; i < arr1.length; i++) {
var v1 = arr1[i];
for (var j = 0; j < arr2.length; j++) {
var v2 = arr2[j];
var value = v1 + v2;
arr.push(value);
};
};
return arr;
}
console.log("方法二=====================>");
console.log(array);
// 方法三:
function serialArray(arr) {
var lengthArr = [];
var productArr = [];
var result = [];
var length = 1;
for (var i = 0; i < arr.length; i++) {
var len = arr[i].length;
lengthArr.push(len);
var product = i === 0 ? 1 : arr[i - 1].length * productArr[i - 1];
productArr.push(product);
length *= len;
}
for (var i = 0; i < length; i++) {
var resultItem = '';
for (var j = 0; j < arr.length; j++) {
resultItem += arr[j][Math.floor(i / productArr[j]) % lengthArr[j]];
}
result.push(resultItem);
}
return result
}
console.log("方法三=====================>");
var myArr = [
['A', 'B', 'C'],
['A1', 'B1', 'C1'],
['A2', 'B2']
];
console.log(serialArray(myArr));
var myArr1 = [
['A', 'B', 'C'],
['A1', 'B1', 'C1', 'D1', 'E1', 'F1'],
['A2', 'B2', 'C2'],
['A3', 'B3', 'C3', 'D3']
];
console.log(serialArray(myArr1));
var myArr2 = [
['A', 'B', 'C'],
['A1', 'B1', 'C1', 'D1'],
['A2', 'B2', 'C2']
];
console.log(serialArray(myArr2));
</script>
</body>
</html>