77. Combinations (not use before, so int I = start, dfs(i+1
其實就是求 subset 的某一層, ex c3取2 => 就是 subset 的 level 2

所以最新的做法是:
但這做法會探索所有可能, 所以比較慢, 但比較好記...
時間複雜度跟 subset 一樣
time: O(2^n) or O(2^n*n) , the subset's count is 2^n
space: O(2^n) or O(2^n*n)
why i <= n - k + 1
For anyone stumped by why this change is necessary, it's because you should not continue exploring (recursing) when you know that there won't be enough numbers left until n to fill the needed k slots. If n = 10, k = 5, and you're in the outermost level of recursion, you choose only i = 1...6 , because if you pick i=7 and go into backTracking() you only have 8,9,10 to pick from, so at most you will get [7,8,9,10]... but we need 5 elements!
time: O( k*c(n, k))
space: O( k*c(n, k))
use <= n
Previous78. Subsets (not use befroe, so int I = start, dfs(i+1)Next90. Subsets II (not use before so int I = start, dfs(i+1), and sort
Last updated
Was this helpful?