JavaScript实现笛卡尔积
注意:本文中所说的集合是指数学上的集合,不是es6里的Set。
整体思路如下:
实现代码如下:
descates.js
/** * Created by Hawk on 2016/6/18. */var DescartesUtils = { /** * 如果传入的参数只有一个数组,求笛卡尔积结果 * @param arr1 一维数组 * @returns {Array} */ descartes1:function(arr1){ // 返回结果,是一个二维数组 var result = []; var i = 0; for (i = 0; i < arr1.length; i++) { var item1 = arr1[i]; result.push([item1]); } return result; }, /** * 如果传入的参数只有两个数组,求笛卡尔积结果 * @param arr1 一维数组 * @param arr2 一维数组 * @returns {Array} */ descartes2: function(arr1, arr2) { // 返回结果,是一个二维数组 var result = []; var i = 0, j = 0; for (i = 0; i < arr1.length; i++) { var item1 = arr1[i]; for (j = 0; j < arr2.length; j++) { var item2 = arr2[j]; result.push([item1, item2]); } } return result; }, /** * * @param arr2D 二维数组 * @param arr1D 一维数组 * @returns {Array} */ descartes2DAnd1D: function(arr2D, arr1D) { var i = 0, j = 0; // 返回结果,是一个二维数组 var result = []; for (i = 0; i < arr2D.length; i++) { var arrOf2D = arr2D[i]; for (j = 0; j < arr1D.length; j++) { var item1D = arr1D[j]; result.push(arrOf2D.concat(item1D)); } } return result; }, descartes3: function(list) { var listLength = list.length; var i = 0, j = 0; // 返回结果,是一个二维数组 var result = []; // 为了便于观察,采用这种顺序 var arr2D = DescartesUtils.descartes2(list[0], list[1]); for (i = 2; i < listLength; i++) { var arrOfList = list[i]; arr2D = DescartesUtils.descartes2DAnd1D(arr2D, arrOfList); } return arr2D; }, //笛卡儿积组合 descartes: function(list) { if (!list) { return []; } if (list.length <= 0) { return []; } if (list.length == 1) { return DescartesUtils.descartes1(list[0]); } if (list.length == 2) { return DescartesUtils.descartes2(list[0], list[1]); } if (list.length >= 3) { return DescartesUtils.descartes3(list); } }};
descartes.html
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="descartes.js"></script></head><body><div id="resultDiv"></div><script type="text/javascript"> var arr1 = [1,2,3]; var arr2 = ["a", "b", "c", "d"]; var list = [arr1, arr2]; var list2 = [ [1,2,3], ["a", "b", "c"], ["A", "B"], ["啊","把","车","的"] ]; var result = DescartesUtils.descartes(list2); var html = ""; for (var i = 0; i < result.length; i++) { html = html + result[i].join(",") + "<br>"; } document.getElementById("resultDiv").innerHTML = html;</script></body></html>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
新闻热点
疑难解答