LeetCode - 解题笔记 - 77 - Combinations

Solution 1

整体来看就是DFS,和之前做到的组合数还不太一样,每个数字能且只能使用一次,因此枚举过程中序列长度也要作为一个终止条件考察。

我只想到了递归做法,非递归做法需要进行选择状态的维护,比较麻烦,就先不考虑实现了。

官方题解还提到了一个额外的剪枝:如果此时最大序列也达不到K长度,提前停止。

官方题解中给出了一个非递归字典序枚举思路,因为我没有想清楚字典序枚举的具体规律,就不实现了。

  • 时间复杂度: O ( ( n k ) × k ) O(\binom{n}{k} \times k) O((kn)×k),所有 ( n k ) \binom{n}{k} (kn)中情形,每种需要k长度规模的遍历
  • 空间复杂度: O ( n ) O(n) O(n),temp数组最大占用为k,但是递归占用可能会达到n
class Solution {
public:
    vector<vector<int>> combine(int n, int k) {
        vector<vector<int>> ans;
        vector<int> temp;
        dfs(1, n, k, temp, ans);
        
        return ans;
    }
    
private:
    void dfs(int pos, int n, int k, vector<int> & temp, vector<vector<int>> & ans) {
        if (temp.size() + (n - pos + 1) < k) {
            // 剪枝,提前终止
            return;
        }
        if (temp.size() == k) {
            ans.emplace_back(temp);
            return;
        }
        
        if (pos == n + 1) {
            // 枚举到了结尾,但是还是不够
            return;
        }
        
        temp.emplace_back(pos);
        dfs(pos + 1, n, k, temp, ans);
        temp.pop_back();
        dfs(pos + 1, n, k, temp, ans);
    }
};

Solution 2

Solution 1的Python实现,注意Python的引用特性的处理。

class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:
        ans = []
        temp = []
        self._dfs(1, n, k, temp, ans)
        
        return ans
        
        
    def _dfs(self, pos: int, n: int, k:int, temp: List[int], ans: List[List[int]]) -> None:
        if len(temp) + (n - pos + 1) < k:
            return
        if len(temp) == k:
            ans.append(copy.deepcopy(temp))
            return
        
        temp.append(pos)
        self._dfs(pos + 1, n, k, temp, ans)
        temp.pop()
        self._dfs(pos + 1, n, k, temp, ans)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值