【LeetCode】Permutation全排列
1. Next Permutation
实现C++的std::next_permutation函数,重新排列范围内的元素,返回按照 字典序 排列的下一个值较大的组合。若其已经是最大排列,则返回最小排列,即按升序重新排列元素。不能分配额外的内存空间。
void nextPermutation(vector<int>& nums) {
next_permutation(nums.begin(), nums.end());
}
全排列 Permutation 问题已经被古人研究透了,参见 Wikipedia page,Next Permutation 有一个经典的简单有效算法,还能解决含有重复元素的全排列问题。
- 从尾端寻找第一个下标 i,使 nums[i] < nums[i + 1]。若这样的 i 不存在,则这个排列已经是降序排列(或者元素全部相同),那么直接逆序得到升序排列。
- 从尾端寻找第一个下标 j,使 nums[i] < nums[j]。若 i 存在,则 j 一定存在且 i < j,因为 j 起码可以 = i + 1。
- 交换 i 和 j 位置的元素。
- 逆序从 i + 1 到结尾的子数组,即求出下一个序列。
e.g.
nums = [6, 3, 4, 9, 8, 7, 1]
i j
- 找到 i = 2,nums[i] = 4。i 右边的一定是降序排列 [9, 8, 7, 1]。
- 从尾端找第一个大于 nums[i] 的数 7,即 nums[j] = 7。
- 交换 4 和 7,[6, 3, 7, 9, 8, 4, 1]。保证交换后的 [6, 3, 7, ......] 一定大于原来的 [6, 3, 4, ......],且可以发现 i 右边的 [9, 8, 4, 1] 仍然是降序排列。
- 对 i 右边进行逆序,得到结果 [6, 3, 7, 1, 4, 8, 9]。
C++实现:
void nextPermutation(vector<int>& nums) {
int j = nums.size() - , i = j;
while (--i >= ) {
if (nums[i] < nums[i + ])
break;
}
if (i != -) {
while (j > i) {
if (nums[j] > nums[i])
break;
j--;
}
swap(nums[i], nums[j]);
}
reverse(nums.begin() + i + , nums.end());
return;
}
2. Permutations
给出一组不含重复数字的数组的全排列。
e.g. [1,2,3] 有全排列:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
我使用递归实现:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> result;
vector<int> item;
add(result, nums, item);
return result;
}
void add(vector<vector<int>> &result, vector<int> v, vector<int> item) {
if (v.empty()) {
result.push_back(item);
return;
}
for (int i = ; i < v.size(); i++) {
item.push_back(v[i]);
vector<int> new_v;
for (int j = ; j < v.size(); j++) {
if (j == i) continue;
new_v.push_back(v[j]);
}
add(result, new_v, item);
item.pop_back();
}
}
看到答案一种更好的方法,使用 swap 函数,不需要创建额外的 item 存储大量重复的数据。
如果 permuteRecursive 函数的 num 参数不用引用,则可以去掉第 15 行的 swap,但这样会创建大量的临时 vector,效率低不少。
而且个人觉得这个方法有点难以理解。真是天才!
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int>> result;
permuteRecursive(result, num, );
return result;
}
void permuteRecursive(vector<vector<int>> &result, vector<int> &num, int begin) {
if (begin == num.size()) {
result.push_back(num);
return;
}
for (int i = begin; i < num.size(); i++) {
swap(num[begin], num[i]);
permuteRecursive(result, num, begin + );
swap(num[begin], num[i]);
}
}
3. Permutations II
给出一组可能含有重复数字的数组的全排列。
e.g. [1,1,2] 有全排列:
[
[1,1,2],
[1,2,1],
[2,1,1]
]
我使用 (1) Next Permutation 的思路,从升序开始逐个计算下一个全排列。
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> result;
sort(nums.begin(), nums.end());
do {
result.push_back(nums);
} while (nextPermutation(nums));
return result;
}
bool nextPermutation(vector<int>& nums) {
int i = nums.size() - ;
while (--i >= && nums[i] >= nums[i + ]);
if (i != -) {
int j = nums.size();
while (--j > i && nums[j] <= nums[i]);
swap(nums[i], nums[j]);
reverse(nums.begin() + i + , nums.end());
return true;
}
return false;
}
别人写了一种类似 (2) 中递归的方法,也比较难以理解。这种情况下不能用引用,以及在递归语句后把交换元素再换回来。
vector<vector<int> > permuteUnique(vector<int> &num) {
sort(num.begin(), num.end());
vector<vector<int>> result;
permuteRecursive(result, num, );
return result;
}
void permuteRecursive(vector<vector<int>> &result, vector<int> num, int begin) {
if (begin == num.size()) {
result.push_back(num);
return;
}
for (int i = begin; i < num.size(); i++) {
if (i > begin && num[i] == num[begin]) continue;
swap(num[begin], num[i]);
permuteRecursive(result, num, begin + );
}
}
【LeetCode】Permutation全排列的更多相关文章
- LeetCode:全排列II【47】
LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
- LeetCode:全排列【46】
LeetCode:全排列[46] 题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2 ...
- 每日一题-——LeetCode(46)全排列
题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...
- LeetCode 47——全排列 II
1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...
- [LeetCode] Permutation in String 字符串中的全排列
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- LeetCode——Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [leetcode]Permutation Sequence @ Python
原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...
随机推荐
- jquery 获取元素(父节点,子节点,兄弟节点),元素筛选
一, js 获取元素(父节点,子节点,兄弟节点) var test = document.getElementById("test"); var parent = test.p ...
- php的Allowed memory size of 134217728 bytes exhausted问题
提示Allowed memory size of 134217728 bytes exhausted,出现这种错误的情况常见的有三种: 0:查询的数据量大. 1:数据量不大,但是php.ini配置的内 ...
- 关于在mac安装安卓的模拟器的一些些那点事情~~~
ook~~自己捣鼓了三天终于安装成功了~妹的~踩了太多的坑~整个人就不好了~ 为了节省大家的时间~所以今天我就将我安装的过程整体思路教给大家!有了思路安装起来就很好了!!!但是 注意的是,也许你会出现 ...
- win10 下载安装eclipse
官网:https://www.eclipse.org 选择下载包 选择下载win 64版本 解压后目录结构如下: 点击运行eclipse
- 远程连接MySQL MySQL的远程连接
在笔记本上安装了mysql, 想测试一下连接池对性能的影响,用了另一台PC来测试一段sql,出现以下错误: jdbc:mysql://10.201.11.128:3306/test Cannot cr ...
- Entity Framework框架 (一)
1. Entity Framework的详细介绍: Entity Framework简称EF,与Asp.net关系与Ado.net关系. Entity Framework是ado.net中的一组支持开 ...
- 学习笔记28—Python 不同数据类型取值方法
1.array数据类型 1)-------> y[i,] 或者 y[i] 2.遍历目录下所有文件夹: def eachFile(filepath): pathDir = os.list ...
- ace后台管理系统扁平化框架
Bootstrap ACE后台管理界面模板(扁平化) 所属分类:后台模板 文件大小:1.22 MB 阅读:236697次 下载:55929次 来源:www.daimajiayuan.com 分享到:更 ...
- 几种RAID介绍(总结)
概念 RAID是Redundent Array of Inexpensive Disks的缩写,简称为“磁盘阵列”.后来RAID中的字母I被改作了Independent,RAID就成了“独立冗余磁盘阵 ...
- python中函数与函数式编程(二)
首先要明白为什么要用到返回值,返回值的作用就是为了分情况来处理下面的程序(个人见解总结) 1.函数返回值 def test1(): pass def test2(): return 0 def tes ...