問題描述
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]
簡單來講,就是輸入?yún)?shù) n和k , n>=k; 問從1-n中取k個數(shù),有多少種可能,并把所有可能通過數(shù)組的方式存起來;
解題思路
分治法
將問題分解成兩個子問題:
- (n-1, k) 不包含n問題;
- (n-1,k-1) 包含n問題
參考代碼:
github