zoukankan      html  css  js  c++  java
  • [LeetCode] Combinations

    题目描述:

    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],
    ]

    解题思路:

     1 class Solution {
     2 public:
     3     vector<vector<int>> combine(int n, int k) {
     4         vector<vector<int>> result;
     5         vector<int> elem;
     6         
     7         combine(n, k, 1, result, elem);
     8         return result;
     9     }
    10 private:
    11     void combine(int n, int k, int cur, vector<vector<int>> &result, vector<int> &elem) {
    12         if (elem.size() == k) {
    13             result.push_back(elem);
    14             return;
    15         }
    16         
    17         for (int i = cur; i <= n; ++i) {
    18             elem.push_back(i);
    19             combine(n, k, i + 1, result, elem);
    20             elem.pop_back();
    21         }
    22     }
    23 };
  • 相关阅读:
    I/O模型
    同步异步与协程
    GIL(全局解释器锁)
    解决pycharm启动慢
    操作系统发展史
    TCP和UDP
    粘包问题
    网络编程
    异常
    常用函数汇总
  • 原文地址:https://www.cnblogs.com/skycore/p/5321436.html
Copyright © 2011-2022 走看看