// 【例10】8人排成一队
// ⑴甲乙必须相邻
function sortStrArr123(str,n){
function inFunction(str){
if (str.length<=2) {
return str.length ==2 ?[str,str[1]+str[0]]:[str]
}
return str.split('').reduce((l,e,i)=>{
return l.concat(inFunction(str.slice(0, i) + str.slice(i + 1)).map(ele=>e+ele))
},[])
}
let arrAll = []
let a1List = inFunction(str.slice(0,n))
let a2List = inFunction(str.slice(n))
function mergerArr(a1List,a2List) {
let arr = []
for (let i = 0; i < a2List.length; i++) {
for (let k = 0; k < a2List[i].length+1; k++) {
for (let j = 0; j < a1List.length; j++) {
let str = a2List[i].split('')
str.splice(k, 0, a1List[j])
arr.push(str.join(''))
}
}
}
return arr
}
arrAll = mergerArr(a1List,a2List)
return arrAll
}
let str8 = "abcdefgh";
let listStr8 = sortStrArr123(str8,2)
console.log(listStr8,';listStr8');
console.log(listStr8.length,';listStr8');
排列组合问题:8人排成一队// ⑴甲乙必须相邻
最新推荐文章于 2024-11-12 11:19:19 发布