SHARE
TWEET

his

a guest Oct 21st, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     vector<vector<int> > permute(vector<int> &num) {
  4.         vector<vector<int> > result;
  5.        
  6.         permuteRecursive(num, 0, result);
  7.         return result;
  8.     }
  9.    
  10.     // permute num[begin..end]
  11.     // invariant: num[0..begin-1] have been fixed/permuted
  12.     void permuteRecursive(vector<int> &num, int begin, vector<vector<int> > &result)    {
  13.         if (begin >= num.size()) {
  14.             // one permutation instance
  15.             result.push_back(num);
  16.             return;
  17.         }
  18.        
  19.         for (int i = begin; i < num.size(); i++) {
  20.             swap(num[begin], num[i]);
  21.             permuteRecursive(num, begin + 1, result);
  22.             // reset
  23.             swap(num[begin], num[i]);
  24.         }
  25.     }
  26. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top