题目:

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]
]

  

题解:

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

Solution 1 ()

class Solution {
public:
void nextPermutation(vector<int>& nums) {
int n = nums.size();
for(int i=n-; i>=; --i) {
if(nums[i]>=nums[i+]) continue;
int j = n-;
for(; j>i; --j) {
if(nums[j]>nums[i]) break;
}
swap(nums[i], nums[j]);
reverse(nums.begin()+i+, nums.end());
return;
}
reverse(nums.begin(), nums.end());
}
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> vv;
vector<int> v = nums;
vv.push_back(v);
nextPermutation(v);
while(v != nums) {
vv.push_back(v);
nextPermutation(v);
}
return vv;
}
};

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

  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 ()

class Solution {
public:
vector<vector<int> > permute(vector<int> &nums) {
vector<vector<int> > result;
permuteRecursive(nums, , result);
return result;
}
// permute nums[begin..end]
// invariant: nums[0..begin-1] have been fixed/permuted
void permuteRecursive(vector<int> &nums, int begin, vector<vector<int> > &result) {
if (begin >= nums.size()) {
// one permutation instance
result.push_back(nums);
return;
}
for (int i = begin; i < nums.size(); i++) {
swap(nums[begin], nums[i]);
permuteRecursive(nums, begin + , result);
// reset
swap(nums[begin], nums[i]);
}
}
};

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

from here

Solution 3 ()

class Solution {
public:
vector<vector<int> > permute(vector<int> &nums) {
vector<vector<int> > res;
vector<int> out;
vector<int> visited(nums.size(), );
permuteDFS(nums, , visited, out, res);
return res;
}
void permuteDFS(vector<int> &nums, int level, vector<int> &visited, vector<int> &out, vector<vector<int> > &res) {
if (level == nums.size()) res.push_back(out);
else {
for (int i = ; i < nums.size(); ++i) {
if (visited[i] == ) {
visited[i] = ;
out.push_back(nums[i]);
permuteDFS(nums, level + , visited, out, res);
out.pop_back();
visited[i] = ;
}
}
}
}
};

  思路: 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 ()

class Solution {
public:
vector<vector<int> > permute(vector<int> &nums) {
if (nums.empty()) return vector<vector<int> >(, vector<int>());
vector<vector<int> > res;
int first = nums[];
nums.erase(nums.begin());
vector<vector<int> > words = permute(nums);
for (auto &a : words) {
for (int i = ; i <= a.size(); ++i) {
a.insert(a.begin() + i, first);
res.push_back(a);
a.erase(a.begin() + i);
}
}
return res;
}
};

【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. String 的fomat方法日期转换

    一.常规类型.字符类型和数值类型的格式说明符的语法如下:%[argument_index$][flags][width][.precision]conversion 可选的 argument_inde ...

  2. erlang中字符编码转换(转)

    转自:http://www.thinksaas.cn/group/topic/244329/ 功能说明: erlang中对各种语言的编码支持不足,此代码是使用erlang驱动了著名的iconv编码库来 ...

  3. Java 并发随身记(一)之 Unsafe类

    最近在看Java并发相关的内容,需要自己整理整理,不然就生疏了.工作2年多,工作时一般注都是框架.消息这些内容,对基础内容比较忽视.闲话不说,既然是并发内容,首先先复习一下Unsafe的内容吧. Un ...

  4. php部分:网页中报表的打印,并用CSS样式控制打印的部分;

    网页中报表的打印,是通过调用window对象中的print()方法实现打印功能的: 调用浏览器本身的打印功能实现打印 <a href="#" onclick="wi ...

  5. squared-error loss is much more repaidly updated than mean-absolute-deviation when searching for splits

    平方差损失能较绝对值差损失更快地更新

  6. iOS 流布局 UICollectionView使用(UICollectionVIew的代理方法)

    UICollectionViewDataSource协议 这个协议主要用于collectionView相关数据的处理,包含方法如下: 设置分区数(这个是可选实现的) - (NSInteger)numb ...

  7. 流畅python学习笔记第十八章:使用asyncio编写服务器

    在这一章中,将使用asyncio写一个TCP服务器.这个服务器的作用是通过规范名称查找Unicode字符,来看下代码: import asyncio from charfinder import Un ...

  8. STO到底是什么?

    最近,链圈谈论最多的就是STO了,那STO到底是什么?现阶段发展得怎么样? 什么是STO STO英文全称Security Token Offering,即证券化通证发行,指在安全法律体系下受到约束的基 ...

  9. COPY SAP 标准gui状态

    [转]如何COPY SAP标准gui状态 1.可以自己建立 2.找到合适的ALV程序,然后找到合适的 gui_statu,进行copy. 但是这个是系统有过自定义开发会方便很多,如果没有,那要找标准程 ...

  10. hdu 2015校赛1002 Dual horsetail (思维题 )

    Dual horsetail Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...