gary@interview:~/interview/coding/77-combinations.md$
$ cat ./coding/77-combinations.md
[Coding]

77. Combinations

────────────────────────────────────────────────────────────

77. Combinations

這一個題目的精神和我們平常窮舉的精神一樣,我們先從最小的數字開始,慢慢地的和比自己大的樹去取組合,列完後,找到次小的數字,重複同樣的動作。

我們從 1 開始,一直到 n + 1 ,看看有幾種組合,每往下探索的時候,我們要從下一個數字來開始看。

class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:

        res = []
        
        def backtrack(start, curr):
            if len(curr) == k:
                res.append(curr.copy())
                return
            
            for i in range(start, n + 1):
                curr.append(i)
                backtrack(i + 1, curr)
                curr.pop()
            
        
        backtrack(1, [])

        return res

--tags#Backtrack
$ ls ./coding/ | grep -v 77-combinations
265. Paint House II256. Paint House143. Reorder List1762. Buildings With an Ocean View
← cd ../codingcd ~