【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 ...
随机推荐
- Javascript 高级程序设计(第3版) - 第01章
2017-05-10 js简介 一个叫“不难登”的人发明的.js的流行是因为 ajax 的关系. js分为三个部分: 核心: ECMAScript 文档对象模型: DOM 浏览器对象模型: BOM 核 ...
- HDU 2426 Interesting Housing Problem(二分图最佳匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2426 题意:每n个学生和m个房间,现在要为每个学生安排一个房间居住,每个学生对于一些房间有一些满意度,如果满意度 ...
- bean的实例化有几种实现方式
三种实例化bean的方式 在spring中有三中实例化bean的方式: 一.使用构造器实例化:(90%通常使用的一个方法) 二.使用静态工厂方法实例化: 三.使用实例化工厂方法实例化. 每种实例化 ...
- java用毫秒数做日期计算的一个踩坑记录
错误示例: Date today = new Date(); Date nextMonth = new Date(today.getTime() + 30* 1000*60*60*24); print ...
- Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
解决方法: 如果安装的是GPU版本 如果你有一个GPU,你不应该关心AVX的支持,因为大多数昂贵的操作将被分派到一个GPU设备上(除非明确地设置).在这种情况下,您可以简单地忽略此警告: import ...
- python类与类的关系
类与类之间的关系(依赖关系,大象与冰箱是依赖关系) class DaXiang: def open(self, bx): # 这里是依赖关系. 想执行这个动作. 必须传递一个bx print(&quo ...
- mysql 和 sqlserver中备份一张表的区别
sqlserver中备份一张表 SELECT * into qa_buglist_bak FROM qa_buglist 不管表的数据有多大,速度很快: mysql中上述语句就无效了,须得新建一张表, ...
- VS IIS Express 支持局域网访问
使用Visual Studio开发Web网页的时候有这样的情况:想要在调试模式下让局域网的其他设备进行访问,以便进行测试.虽然可以部署到服务器中,但是却无法进行调试,就算是注入进程进行调试也是无法达到 ...
- ubuntu 18.04编译opencv3.4.3 with python3.6 cuda9.2 gdal
惭愧,之前一直没在linux下编译过opencv,也没用过纯命令行版的cmake,现在咬牙编译一次.其实感觉还凑合. opencv官网文档还是那么烂:https://docs.opencv.org/m ...
- lua_call/lua_pcall/xpcall
vs2013+lua5.3.3 1.涉及函数 主要C函数:lua_call和lua_pcall 主要lua函数xpcall 2.正常使用lua_call ①hello.lua文件内容 function ...