Combinations

深感自己智商不足啊,看了好久才看明白。

void dfs(int n, int k, int start, int cur, vector<vector<int>> &result, vector<int> &path)
      {
          if (cur == k)
          {
              result.push_back(path);
              return;
          }
          for (int i = start; i < n; i++)
          {
              path.push_back(i);
              dfs(n, k, i + 1, cur + 1, result, path);
              path.pop_back();
          }
      }

      vector<vector<int>>combine(int n, int k)
      {
          vector<vector<int>>result;
          vector<int> path;

          dfs(n, k, 1, 0, result, path);
          return result;
      }
View Code

 

posted @ 2016-07-16 19:15  牧马人夏峥  阅读(113)  评论(0编辑  收藏  举报