Leetcode 46. Permutations

46. Permutations

  • Total Accepted: 112369
  • Total Submissions: 300018
  • Difficulty: Medium

 

Given a collection of distinct numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:

[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

 

 

思路:回溯法,具体见代码。

方法一是递归的回溯法,比较典型。

 

方法二比方法一更加快,也更加巧妙。

 

方法三是递推的回溯法,比如[1,2,3]的结果:

1.有1个插入位置,只插入第1个元素:[1]

2.有2个插入位置,只插入第2个元素:

由[1]插入2变化而来:[2,1] [1,2]

3.有3个插入位置,只插入第3元素:

由[2,1]插入3变化而来:[3,2,1] [2,3,1] [2,1,3]

由[1,2]插入3变化而来:[3,1,2] [1,3,2] [1,2,3]

所以结果是[3,2,1] [2,3,1] [2,1,3] [3,1,2] [1,3,2] [1,2,3]

 

只要注意如何设定生成顺序,依次产生有效结果即可。

 

代码:

方法一:28 ms

 1 class Solution {
 2 public:
 3     void permute(vector<vector<int> > &res,vector<int> nums,vector<int> v,unordered_set<int> used,int cur){
 4         if(cur==nums.size()){
 5             res.push_back(v);
 6             return;
 7         }
 8         for(int i=0;i<nums.size();i++){
 9             if(used.erase(nums[i])){
10                 v.push_back(nums[i]);
11                 permute(res,nums,v,used,cur+1);
12                 v.pop_back();
13                 used.insert(nums[i]);
14             }
15         }
16     }
17     vector<vector<int> > permute(vector<int>& nums) {
18         unordered_set<int> used;
19         for(int i=0;i<nums.size();i++){
20             used.insert(nums[i]);
21         }
22         vector<vector<int> > res;
23         vector<int> v;
24         permute(res,nums,v,used,0);
25         return res;
26     }
27 };

 

 

方法二:12 ms

参考https://discuss.leetcode.com/topic/5881/my-elegant-recursive-c-solution-with-inline-explanation/2

 1 class Solution {
 2 public:
 3     vector<vector<int> > permute(vector<int> &num) {
 4         vector<vector<int> > result;
 5         permuteRecursive(num, 0, result);
 6         return result;
 7     }
 8     
 9     // permute num[begin..end]
10     // invariant: num[0..begin-1] have been fixed/permuted
11     void permuteRecursive(vector<int> &num, int begin, vector<vector<int> > &result)    {
12         if (begin >= num.size()) {
13             // one permutation instance
14             result.push_back(num);
15             return;
16         }
17         
18         for (int i = begin; i < num.size(); i++) {
19             swap(num[begin], num[i]);
20             permuteRecursive(num, begin + 1, result);
21             // reset
22             swap(num[begin], num[i]);
23         }
24     }
25 };

 

方法三:40 ms

 1 class Solution {
 2 public:
 3     vector<vector<int> > permute(vector<int>& nums) {
 4         vector<vector<int> > res;
 5         vector<int> v(1,nums[0]);
 6         res.push_back(v);
 7         for(int i=1;i<nums.size();i++){
 8             int size=res.size();
 9             for(int j=0;j<size;j++){
10                 v=res[0];
11                 res.erase(res.begin());
12                 for(int k=0;k<=v.size();k++){
13                    v.insert(v.begin()+k,nums[i]);
14                    res.push_back(v);
15                    v.erase(v.begin()+k);
16                 }
17             }
18         }
19         return res;
20     }
21 };

 

 

posted @ 2016-08-08 11:14  Deribs4  阅读(168)  评论(0编辑  收藏  举报