zoukankan      html  css  js  c++  java
  • 30 Day Challenge Day 14 | Leetcode 77. Combinations

    题解

    Medium | Backtracking

    经典的排列组合题,使用回溯法。必须熟练。

    class Solution {
    public:
        vector<vector<int>> combine(int n, int k) {
            vector<vector<int>> combinations;
            vector<int> combination;
            helper(n, k, 1, combination, combinations);
            return combinations;
        }
        
        void helper(int n, int k, int pos, vector<int>& combination, vector<vector<int>>& combinations) {
            if(k == 0) {
                combinations.push_back(combination);
                return;
            }
            
            for(int i = pos; i <= n; i++) {
                combination.push_back(i);
                helper(n, k-1, i+1, combination, combinations);
                combination.pop_back();
            }
        }
    };
    
  • 相关阅读:
    搜索区间
    搜索插入位置
    旋转排序数组
    搜索二维矩阵
    njnja 安装
    rpmbuild打包
    snappy 安装
    mysql8 安装
    re2c安装
    make 安装
  • 原文地址:https://www.cnblogs.com/casperwin/p/13748896.html
Copyright © 2011-2022 走看看