2 回答
TA贡献1836条经验 获得超4个赞
function group(total, size) {
var groupList = []
var arr = []
for (let i = 0; i < total; i++) {
arr.push(i + 1)
}
(function (arr, size, group) {
var arrLen = arr.length
if (size > arrLen) return
if (size == arrLen) {
groupList.push([].concat(group, arr))
} else {
for (var i = 0; i < arrLen; i++) {
var newGroup = [].concat(group)
newGroup.push(arr[i])
if (size == 1) {
groupList.push(newGroup)
} else {
var newArr = [].concat(arr)
newArr.splice(0, i + 1)
arguments.callee(newArr, size - 1, newGroup)
}
}
}
})(arr, size, []);
return groupList;
}
console.log(group(3, 2))
console.log(group(4, 2))
console.log(group(4, 3))
console.log(group(5, 2))
添加回答
举报