【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 ...
随机推荐
- 原生JS取代一些JQuery方法的简单实现
原生JS取代一些JQuery方法的简单实现 下面小编就为大家带来一篇原生JS取代一些JQuery方法的简单实现.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧 1.选 ...
- java web 工程更改名字
如图: 将工程名字struts2Project02更改为struts2Project03,步骤如下: 1. 右键工程名字,选中properties,如图 2.更改项目名字 3.第2步已经真正把项目名字 ...
- git切换分支报错:error: pathspec 'origin/XXX' did not match any file(s) known to git
项目上有一个分支test,使用git branch -a看不到该远程分支,直接使用命令git checkout test报错如下: error: pathspec 'origin/test' did ...
- BZOJ 4826 【HNOI2017】 影魔
题目链接:影魔 这道题就是去年序列的弱化版啊…… 我们枚举最大值的位置\(i\),找出左边第一个比\(a_i\)大的位置\(l\),右边第一个比\(a_i\)大的位置\(r\),然后我们分开考虑一下\ ...
- char,String,int类型互转
1.ascci码对应转换 字符,对应的ascii(其实是UTF-16)码: char c='a'; int k=(int) c; 结果k=97 数字,对应的字符: int k=9 ...
- 首篇 sdk 之 AlertDialog
带着十足的干劲,用着有限的英语水平,我们来看看sdk里docs里的AlertDialog: AlertDialog SDK 原文描述:A dialog that can show a title, u ...
- 30秒了解Excel的前世今生
JS 大家日常工作离不开的Excel,实际上脱胎于1979年问世的Apple II电脑上的一款杀手级应用VisiCalc. VisiCalc在问世后的6年间,以100美金一份的价格卖出了超过70万份, ...
- 四个session
- DOCTYPE的作用以及标准模式和兼容模式的区别
<!doctype>声明必须处于HTML文档的头部,在<html>标签之前,告知浏览器的解析器用什么文档标准解析这个文档.DOCTYPE不存在或格式不正确会导致文档以兼容模式呈 ...
- ionic 页面传递参数
1.使用AngularJS自带的$cacheFactory服务 $cacheFactory 从字面直译即为缓存工厂,可以用它来生成缓存对象,缓存对象以key-value的方式进行数据的存储,在整个应用 ...