permutation

1
2
3
4
5
6
7
8
9
10
void permute(vector<int>& nums, int start, int end, set<vector<int>>& setRes){
if(start == end){
setRes.insert(nums);
}
for(int i = start; i <= end; i++){
swap(nums[start], nums[i]);
permute(nums, start + 1, end, setRes);
swap(nums[start] , nums[i]);
}
}
  • The details of this code

    • the start is fixed in each recursion, it changes when calls permute function
    • the elements following the start change in each recursion and exchange their positions with the start.
1
permute(nums, start + 1, end, setRes);