Problem Statement

Pattern:


Solution

vector<vector<int>> ans;
void solve(vector<int> &nums, int t) {
    if (t == nums.size()-1) {
        ans.push_back(nums);
        return;
    }
 
    for(int i = t; i < nums.size() ; i++) {
        swap(nums[t], nums[i]);
        solve(nums, t+1);
        swap(nums[t], nums[i]);
    }
}
 
 
vector<vector<int>> permute(vector<int>& nums) {
    ans.clear();
    solve(nums, 0);
    return ans;
}

TC : n! SC : n!

Notes