nikunjsoni

46

Jul 4th, 2021
116
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>& nums) {
  4.         vector<vector<int>> ans;
  5.         permute(nums, 0, ans);
  6.         return ans;
  7.     }
  8.    
  9.     void permute(vector<int> &nums, int idx, vector<vector<int>> &ans){
  10.         if(idx == nums.size()){
  11.             ans.push_back(nums);
  12.         }
  13.         for(int i=idx; i<nums.size(); i++){
  14.             swap(nums[i], nums[idx]);
  15.             permute(nums, idx+1, ans);
  16.             swap(nums[i], nums[idx]);
  17.         }
  18.     }
  19. };
RAW Paste Data