You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
lavos/cap3/ex10.go

29 lines
552 B

package cap3
// Ex10 从0~n的整数中取r个不同的数做一组,求所有可能的组合
func Ex10(n, r int, callback func(nums []int)) int {
if r > n {
return 0
}
count := 0
combine := make([]int, r)
var findCombine func(m, k int)
findCombine = func(n, k int) {
// 从n开始往下遍历组合
for i := n; i >= k; i-- {
combine[k-1] = i
if k > 1 {
// k-1和n-1,继续深入遍历,处理子问题
findCombine(i-1, k-1)
} else {
count++
callback(combine)
}
}
}
findCombine(n, r)
return count
}