YEZAELP

LeetCode: Combinations

Dec 1st, 2021 (edited)
637
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.    
  4.     vector < vector <int> > ans;
  5.     int mark[20 + 10];
  6.     int N, K;
  7.    
  8.     void f(int k, int prev){
  9.         if(k == K){
  10.             vector <int> cur;
  11.             for(int i=1;i<=N;i++)
  12.                 if(mark[i]) cur.push_back(i);
  13.             ans.push_back(cur);
  14.         }
  15.         for(int i=prev+1;i<=N;i++){
  16.             if(!mark[i]){
  17.                 mark[i] = true;
  18.                 f(k + 1, i);
  19.                 mark[i] = false;
  20.             }
  21.         }
  22.     }
  23.    
  24.     vector<vector<int>> combine(int n, int k) {
  25.         N = n, K = k;
  26.         f(0, 0);
  27.         return ans;
  28.     }
  29. };
RAW Paste Data