题目:

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

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

  1. [
  2. [1,2,3],
  3. [1,3,2],
  4. [2,1,3],
  5. [2,3,1],
  6. [3,1,2],
  7. [3,2,1]
  8. ]

  

题解:

  之前解过Next Permutation,故这个题可以重复调用nextPermutation函数产生全排列

Solution 1 ()

  1. class Solution {
  2. public:
  3. void nextPermutation(vector<int>& nums) {
  4. int n = nums.size();
  5. for(int i=n-; i>=; --i) {
  6. if(nums[i]>=nums[i+]) continue;
  7. int j = n-;
  8. for(; j>i; --j) {
  9. if(nums[j]>nums[i]) break;
  10. }
  11. swap(nums[i], nums[j]);
  12. reverse(nums.begin()+i+, nums.end());
  13. return;
  14. }
  15. reverse(nums.begin(), nums.end());
  16. }
  17. vector<vector<int>> permute(vector<int>& nums) {
  18. vector<vector<int>> vv;
  19. vector<int> v = nums;
  20. vv.push_back(v);
  21. nextPermutation(v);
  22. while(v != nums) {
  23. vv.push_back(v);
  24. nextPermutation(v);
  25. }
  26. return vv;
  27. }
  28. };

  其实这个问题很容易想到递归的解法

  for the length of n, the permutations can be generated by
(1) Swap the 1st element with all the elements, including itself.
(2) Then the 1st element is fixed, Go to the next element.
(3) Until the last element is fixed. Output.
It's more clear in the figure above. The key point is to make the big problem into smaller problem, here is how to convert the length n permutation into length n-1 permutation problem. from here and here

Solution 2 ()

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

  利用深度优先遍历遍历所有情况,DFS方法,用到一个数组来标记某个数字是否访问过,然后在DFS递归函数从的循环应从头开始,而不是从level开始,这是和Combinations 组合项不同的地方。

from here

Solution 3 ()

  1. class Solution {
  2. public:
  3. vector<vector<int> > permute(vector<int> &nums) {
  4. vector<vector<int> > res;
  5. vector<int> out;
  6. vector<int> visited(nums.size(), );
  7. permuteDFS(nums, , visited, out, res);
  8. return res;
  9. }
  10. void permuteDFS(vector<int> &nums, int level, vector<int> &visited, vector<int> &out, vector<vector<int> > &res) {
  11. if (level == nums.size()) res.push_back(out);
  12. else {
  13. for (int i = ; i < nums.size(); ++i) {
  14. if (visited[i] == ) {
  15. visited[i] = ;
  16. out.push_back(nums[i]);
  17. permuteDFS(nums, level + , visited, out, res);
  18. out.pop_back();
  19. visited[i] = ;
  20. }
  21. }
  22. }
  23. }
  24. };

  思路: from here

当n=1时,数组中只有一个数a1,其全排列只有一种,即为a1

当n=2时,数组中此时有a1a2,其全排列有两种,a1a2和a2a1,那么此时我们考虑和上面那种情况的关系,我们发现,其实就是在a1的前后两个位置分别加入了a2

当n=3时,数组中有a1a2a3,此时全排列有六种,分别为a1a2a3, a1a3a2, a2a1a3, a2a3a1, a3a1a2, 和 a3a2a1。那么根据上面的结论,实际上是在a1a2和a2a1的基础上在不同的位置上加入a3而得到的。

_ a_ a_ : a3a1a2, a1a3a2, a1a2a3

_ a_ a_ : a3a2a1, a2a3a1, a2a1a3

Solution 4 ()

  1. class Solution {
  2. public:
  3. vector<vector<int> > permute(vector<int> &nums) {
  4. if (nums.empty()) return vector<vector<int> >(, vector<int>());
  5. vector<vector<int> > res;
  6. int first = nums[];
  7. nums.erase(nums.begin());
  8. vector<vector<int> > words = permute(nums);
  9. for (auto &a : words) {
  10. for (int i = ; i <= a.size(); ++i) {
  11. a.insert(a.begin() + i, first);
  12. res.push_back(a);
  13. a.erase(a.begin() + i);
  14. }
  15. }
  16. return res;
  17. }
  18. };

【LeetCode】046. Permutations的更多相关文章

  1. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  2. 【LeetCode】46. Permutations 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:库函数 方法二:递归 方法三:回溯法 日期 题目地址:h ...

  3. 【LeetCode】47. Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  4. 【LeetCode】46. Permutations (2 solutions)

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  5. 【LeetCode】047. Permutations II

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  6. 【一天一道LeetCode】#47. Permutations II

    一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...

  7. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  8. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  9. 【LeetCode】分治法 divide and conquer (共17题)

    链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...

随机推荐

  1. spark on yarn 配置history server

    spark在yarn模式下配置history server 1.建立hdfs文件– hadoop fs -mkdir /user/spark– hadoop fs -mkdir /user/spark ...

  2. java 面试总结

    1.static变量与实体变量的差别? static是静态变量,static能够通过类名直接訪问 内存方面的不同:static在定义的时候jvm就会分配空间, 而实体变量仅仅有在创建对象的时候才会去分 ...

  3. 抽钻石vs中奖门 概率问题

    在概率问题中,假设跟着日常经验与感觉走.常常会得到错误的答案.以下"抽钻石"的故事非常可以说明这一点. 题目一:某天电视台举办了这种一个游戏节目.主持人首先拿出三个盒子.已知这三个 ...

  4. EF获取DbContext中已注册的所有实体类型

    /// <summary> /// 获取DbContext中已注册的实体类型 /// </summary> /// <typeparam name="T&quo ...

  5. Cocos2d-x 3.0final 终结者系列教程16-《微信飞机大战》实现

    看到cocos2d-x推出了3.1版本号,真是每月一次新版本号,速度. 另一个好消息就是http://cn.cocos2d-x.org/上线了,祝贺!啥时候把我的视频和教程放上去呢?!! . 视频下载 ...

  6. python 基础 9.3 mysql 数据操作

    #/usr/bin/python #coding=utf-8 #@Time   :2017/11/21 0:20 #@Auther :liuzhenchuan #@File   :mysql 数据操作 ...

  7. 【BZOJ3834】[Poi2014]Solar Panels 分块好题

    [BZOJ3834][Poi2014]Solar Panels Description Having decided to invest in renewable energy, Byteasar s ...

  8. nexus搭建maven私服及私服jar包上传和下载

    nexus搭建maven私服及私服jar包上传和下载 标签: nexus管理maven库snapshot 2017-06-28 13:02 844人阅读 评论(0) 收藏 举报 分类: Maven(1 ...

  9. input file 选择Excel

    说明:开发环境 vs2012 asp.net mvc4 c# ,使用file 选择Excel文件 传到后台 使用Aspose.Cells获取Excel sheet页的名称 1.HTML代码 <% ...

  10. gridcontrol 之标题 GroupPanel设置 (标题设置,屏蔽右键)

    GroupPanel设置 例如gridcontrol显示标题:“gridcontrol小例子” gridView1.GroupPanelText="gridcontrol小例子"; ...