leetcode 46. Full permutation

Given a sequence with no repeating numbers, return all possible permutations of it.

Example:

Input: [1,2,3]
output:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
] 

Ideas: Recursive ideas to achieve, that is, fix the first digit, and perform the same algorithm on the remaining numbers, until all an array is traversed.
Use vector<vector<int>>& ans to store the fully arrayed array, use begin to represent the starting position that needs to be fully arrayed, and end to represent the end position that needs to be fully arrayed.
It is obvious that the complexity of this algorithm is O(n!), the first bit needs to be swapped n times, the second bit needs to be swapped n-1 times, ....
The fix mentioned above is actually a recursive swap of the i-th bit. It
can be found that there are two swaps in the program ( The call of nums, begin, i), first of all, this is to exchange the numbers between begin and i in nums. In addition, calling twice is to restore the array to ensure the correctness of the next arrangement.
Use 1, 2, and 3 to explain the program. At the beginning, begin=0, end = 2; (end is always the last digit of the array subscript)
permute(nums, ans, 0, 2)
  i=0; //swap the first bit
  swap(nums,0,0); nums=1,2,3
  permute(nums, ans, 1, 2)
    i=1;
    swap(nums, 1, 1); nums=1,2,3
    permute(nums, ans, 2, 2)
      ans.push(nums); ans=[[1,2,3]]
    // After completing a complete permutation, restore the array to the previous state. This complete permutation is for each subcolumn in the recursion, not for the entire sequence.
    swap(nums, 1, 2);                   nums=1,2,3
    i=2;
    swap(nums, 1, 2);                   nums=1,3,2
    permute(nums, ans, 2, 2)
      ans.push(nums);                  ans=[[1,2,3],[1,3,2]]
    swap(nums, 1, 2);                   nums=1,2,3
  swap(nums, 0, 0)                      nums=1,2,3
  i=1;
  swap(nums, 0, 1);                     nums=2,1,3
  permute(nums, ans, 1, 2);
    i=1;
    swap(nums, 1, 1);                   nums = 2,1,3
    permute(nums, ans, 2, 2);
      ans.push_back(nums);               ans=[[1,2,3], [1,3,2], [2,1,3]]
    swap(nums, 1, 1);                   nums = 2,1,3
    i=2;
    swap(nums, 1, 2);                   nums=2,3,1
    permute(nums, ans, 2, 2);
      ans.push_back(nums);               ans=[[1,2,3], [1,3,2], [2,1,3], [2,3,1]]
    swap(nums, 1, 2);                    nums=2,1,3
  swap(nums, 0, 1);                    nums=1,2,3
  i=2;
  swap(nums, 0, 2);                      nums=3,2,1
  permute(nums, ans, 1, 2);
    i = 1;
    swap(nums, 1, 1);                    nums=3,2,1
    permute(nums, ans, 2, 2);
      ans.push_back(nums);               ans=[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,2,1]]
    swap(nums, 1, 1);                   nums=3,2,1
    i = 2;
    swap(nums, 1, 2);                  nums=3,1,2
    permute(nums, ans, 2, 2);
      ans.push_back(nums); ans=[[1,2,3],[1,3,2],[2,1,3],[3,2, 1],[3,1,2]]
    swap(nums, 1, 2); nums=3,2,1
  swap(nums, 0, 2); nums=1,2,3
  
Combining the above recursive analysis and the following It is easy to understand this recursive algorithm
 1 class Solution {
 2 public:
 3     void permute(vector<int>&nums, vector<vector<int>>& ans, int begin, int end){
 4         if(begin == end){
 5             ans.push_back(nums);
 6             return;
 7         }else{
 8             for(int i = begin; i <= end; i++){
 9                 swap(nums, begin, i);  
10                 permute(nums, ans, begin+1, end);
11                 swap(nums, begin, i);  //还原数组
12             }
13         }
14     }
15     
16     void swap(vector<int>& nums, int k, int i){
17         int temp = nums[k];
18         nums[k] = nums[i];
19         nums[i] = temp;
20     }
21     
22     vector<vector<int>> permute(vector<int>& nums) {
23         vector<vector<int>> ans;
24         int len = nums.size()-1;
25         permute(nums, ans, 0, len);
26         return ans;
27     }
28 };

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325342631&siteId=291194637