|
发表于 2020-1-21 13:08:06
|
显示全部楼层
浙江省温州市
手头没有易的代码..给你一份JS的,你自己翻译一下,或者直接调用
- var a = ["1", "2", "3"];
- arrayCombine(a);
- function arrayCombine(targetArr) {
- if (!targetArr || !targetArr.length) {
- return [];
- }
- var len = targetArr.length;
- var resultArrs = [];
- // 所有组合
- for (var n = 1; n < len; n++) {
- var flagArrs = getFlagArrs(len, n);
- while (flagArrs.length) {
- var flagArr = flagArrs.shift();
- var combArr = [];
- var combStr = "";
- for (var i = 0; i < len; i++) {
- //console.log(flagArr[i]);
- if(flagArr[i]){
- combStr += targetArr[i]+'|';
- }
- }
- combStr = combStr.substr(0,combStr.length-1);
- resultArrs.push(combStr);
- }
- }
- return resultArrs;
- };
- function getFlagArrs(m, n) {
- if (!n || n < 1) {
- return [];
- }
- var resultArrs = [],
- flagArr = [],
- isEnd = false,
- i, j, leftCnt;
- for (i = 0; i < m; i++) {
- flagArr[i] = i < n ? 1 : 0;
- }
- resultArrs.push(flagArr.concat());
- while (!isEnd) {
- leftCnt = 0;
- for (i = 0; i < m - 1; i++) {
- if (flagArr[i] == 1 && flagArr[i + 1] == 0) {
- for (j = 0; j < i; j++) {
- flagArr[j] = j < leftCnt ? 1 : 0;
- }
- flagArr[i] = 0;
- flagArr[i + 1] = 1;
- var aTmp = flagArr.concat();
- resultArrs.push(aTmp);
- if (aTmp.slice(-n).join("").indexOf('0') == -1) {
- isEnd = true;
- }
- break;
- }
- flagArr[i] == 1 && leftCnt++;
- }
- }
- return resultArrs;
- };
复制代码 |
|