题目:

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. Laravel 5.4---后端数据分页和前端显示分页结果

    后端数据(Eloquent 模型)分页 事先建立好Eloquent 模型和Controller 还有 前台的View.可以参考我之前的文章:Laravel建站03--建立前台文章列表和文章详情 在co ...

  2. ie6中利用jquery居中

    1.利用jquery居中代码 <script type="text/javascript"> $hwidth=parseInt($(window).width()); ...

  3. Delphi列表控件TListView定位到某一行。

    ListView1.Item[100].Focused = true; //定位到索引为100的行ListView1.Item[100].Selected = true; ListView1.Item ...

  4. Centos 6.X noVNC+websockify 实现webvnc

    文章参考:https://github.com/kanaka/noVNC http://www.cnblogs.com/yanghuahui/p/3574388.html 工作原理: noVNC 可以 ...

  5. 【转】【selenium+Python WebDriver】之元素定位不到解决办法

    感谢: 煜妃的<Python+Selenium定位不到元素常见原因及解决办法(报:NoSuchElementException)> ClassName定位报错问题:<[Python] ...

  6. 献给写作者的 Markdown 新手指南及语法

    烈推荐所有写作者学习和掌握该语言.为什么?可以参考: 『为什么作家应该用 Markdown 保存自己的文稿』. 『Markdown写作浅谈』 让你专注于文字而不是排版. 标题 只需要在文本前面加上 # ...

  7. Android App 启动页(Splash)黑/白闪屏现象产生原因与解决办法(转)

    转载: Android App 启动页(Splash)黑/白闪屏现象产生原因与解决办法   首先感谢博主分享,本文作为学习记录 惊鸿一瞥 微信的启动页,相信大家都不陌生. 不知道大家有没有发现一个现象 ...

  8. 在另一个线程中无法用((CMainFrame *)AfxGetMainWnd())

    一个vc6的项目放到vc8下重新编译这里死活过不去 查了些资料无果后来翻到一句老外的回答 If AfxGetMainWnd is called from the application’s prima ...

  9. Python—发邮件总结

    来自: http://my.oschina.net/jhao104/blog/613774 1.登录SMTP服务器 首先使用网上的方法(这里使用163邮箱,smtp.163.com是smtp服务器地址 ...

  10. pycharm连git和gitee

    http://www.cnblogs.com/feixuelove1009/p/5955332.html https://www.58jb.com/html/171.html