Permutations Problem & Solution

See the permutations problem on LeetCode.

C++ Solution

#pragma GCC optimize("Ofast")
#pragma GCC optimization("max-inline-insns-recursive-auto")

static const int _=[](){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

class Solution {
public:
  vector<vector<int>> permute(vector<int>& nums) {
    vector<vector<int>> result;
    
    help(0, nums, result);
    
    return result;
  }
  
private:
  void help(int pivot, vector<int>& nums, vector<vector<int>>& result) {
    if (pivot == nums.size()) {
      result.push_back(vector<int>(nums));    
      return;
    }
    
    for (int i = pivot; i < nums.size(); ++i) {
      swap(nums[i], nums[pivot]);
      help(pivot + 1, nums, result);
      swap(nums[i], nums[pivot]);
    }
  }
};