编程语言
首页 > 编程语言> > 力扣算法题—046全排列

力扣算法题—046全排列

作者:互联网

  1 #include "000库函数.h"
  2 
  3 
  4 //使用排序,感觉在作弊,但是题目没说不可以
  5 //24ms
  6 class solution {
  7 public:
  8     vector<vector<int>> permute(vector<int>& nums) {
  9         vector<vector<int>>res;
 10         if (nums.empty())return res;
 11         sort(nums.begin(), nums.end());//排序
 12         do {
 13             res.push_back(nums);                
 14         } while (next_permutation(nums.begin(), nums.end()));
 15         return res;
 16     }
 17 };
 18 
 19 //使用递归
 20 class Solution {
 21 public:
 22     vector<vector<int>> permute(vector<int>& nums) {
 23         vector<vector<int>>Res;
 24         if (nums.empty())return Res;
 25         Combin(Res, nums, 0);//回溯
 26         return Res;
 27     }
 28     void Combin(vector<vector<int>>&Res, vector<int>v, int s) {
 29         if (s >= v.size()) //此处不return,在for中结束后自动结束
 30             Res.push_back(v);
 31             
 32         for (int i = s; i < v.size(); ++i) {
 33             swap(v[s], v[i]);//交换
 34             Combin(Res, v, s + 1);//回溯
 35             swap(v[s], v[i]);//换回来
 36         }
 37     }
 38 
 39 };
 40 
 41 ////使用DFS递归
 42 class Solution {
 43 public:
 44     vector<vector<int>> permute(vector<int>& num) {
 45         vector<vector<int>> res;
 46         vector<int> out, visited(num.size(), 0);
 47         permuteDFS(num, 0, visited, out, res);
 48         return res;
 49     }
 50     void permuteDFS(vector<int>& num, int level, vector<int>& visited, vector<int>& out, vector<vector<int>>& res) {
 51         if (level == num.size()) { res.push_back(out); return; }//数字大小达到要求
 52         for (int i = 0; i < num.size(); ++i) {
 53             if (visited[i] == 1) continue;
 54             visited[i] = 1;//该数据已经选用
 55             out.push_back(num[i]);
 56             permuteDFS(num, level + 1, visited, out, res);
 57             out.pop_back();//回溯
 58             visited[i] = 0;
 59         }
 60     }
 61 };
 62 
 63 //当n = 1时,数组中只有一个数a1,其全排列只有一种,即为a1
 64 //
 65 //当n = 2时,数组中此时有a1a2,其全排列有两种,a1a2和a2a1,那么此时我们考虑和上面那种情况的关系,我们发现,其实就是在a1的前后两个位置分别加入了a2
 66 //
 67 //当n = 3时,数组中有a1a2a3,此时全排列有六种,分别为a1a2a3, a1a3a2, a2a1a3, a2a3a1, a3a1a2, 和 a3a2a1。那么根据上面的结论,实际上是在a1a2和a2a1的基础上在不同的位置上加入a3而得到的。
 68 //
 69 //_ a1 _ a2 _ : a3a1a2, a1a3a2, a1a2a3
 70 //
 71 //_ a2 _ a1 _ : a3a2a1, a2a3a1, a2a1a3
 72 class Solution {
 73 public:
 74     vector<vector<int>> permute(vector<int>& num) {
 75         vector<vector<int>> res;
 76         if (num.empty()) return res;        
 77         int first = num[0];
 78         num.erase(num.begin());//删除此数字
 79         vector<vector<int>> words = permute(num);
 80         for (auto &a : words) {
 81             for (int i = 0; i <= a.size(); ++i) {
 82                 a.insert(a.begin() + i, first);
 83                 res.push_back(a);
 84                 a.erase(a.begin() + i);
 85             }
 86         }
 87         return res;
 88     }
 89 };
 90 
 91 void T046() {
 92     vector<int>n;
 93     Solution s;
 94     n = { 1,2,3 };
 95     for (auto &a : s.permute(n)) {
 96         for (auto b : a)
 97             cout << b << "  ";
 98         cout << endl;
 99     }
100 
101 }

 

标签:num,nums,res,return,力扣,算法,vector,Res,046
来源: https://www.cnblogs.com/zzw1024/p/10609963.html