Lintcode 152. Combinations

题目链接:https://www.lintcode.com/problem/combinations/description

func combine(n int, k int) [][]int {
	// write your code here
	var rst [][]int;
	var solution []int;
	helper(&rst, solution, n, k, 1);
	return rst;
}

func helper(rst *[][]int, solution []int, n int, k int, start int) {
	if len(solution) == k {
		tmp := []int{};
		tmp = append(tmp, solution...)
		*rst = append(*rst, tmp);
		//fmt.Println("rst",  rst);
		//fmt.Println("solution", solution);
		return;
	}
	var i int;
	for i = start; i <= n; i++ {
		var addNum = append(solution, i);
		helper(rst, addNum, n, k, i+1);
	}
}

func main() {
	var rst [][]int = combine(10, 7);
	fmt.Println(rst);
}

你可能感兴趣的:(Go,Lintcode)