1. Next Permutation

实现C++的std::next_permutation函数,重新排列范围内的元素,返回按照 字典序 排列的下一个值较大的组合。若其已经是最大排列,则返回最小排列,即按升序重新排列元素。不能分配额外的内存空间。

void nextPermutation(vector<int>& nums) {
next_permutation(nums.begin(), nums.end());
}

全排列 Permutation 问题已经被古人研究透了,参见 Wikipedia page,Next Permutation 有一个经典的简单有效算法,还能解决含有重复元素的全排列问题。

  1. 从尾端寻找第一个下标 i,使 nums[i] < nums[i + 1]。若这样的 i 不存在,则这个排列已经是降序排列(或者元素全部相同),那么直接逆序得到升序排列。
  2. 从尾端寻找第一个下标 j,使 nums[i] < nums[j]。若 i 存在,则 j 一定存在且 i < j,因为 j 起码可以 = i + 1。
  3. 交换 i 和 j 位置的元素。
  4. 逆序从 i + 1 到结尾的子数组,即求出下一个序列。

e.g.

nums = [6, 3, 4, 9, 8, 7, 1]
i j
  1. 找到 i = 2,nums[i] = 4。i 右边的一定是降序排列 [9, 8, 7, 1]。
  2. 从尾端找第一个大于 nums[i] 的数 7,即 nums[j] = 7。
  3. 交换 4 和 7,[6, 3, 7, 9, 8, 4, 1]。保证交换后的 [6, 3, 7, ......] 一定大于原来的 [6, 3, 4, ......],且可以发现 i 右边的 [9, 8, 4, 1] 仍然是降序排列。
  4. 对 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全排列的更多相关文章

  1. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

  2. LeetCode:全排列【46】

    LeetCode:全排列[46] 题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2 ...

  3. 每日一题-——LeetCode(46)全排列

    题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...

  4. LeetCode 47——全排列 II

    1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...

  5. [LeetCode] Permutation in String 字符串中的全排列

    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...

  6. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  7. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  8. LeetCode——Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  9. [leetcode]Permutation Sequence @ Python

    原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...

随机推荐

  1. Entity Framework Core一键生成实体命令

    打开Vs中工具——Nug包管理器——程序包管理控制台 设置启动项目为存储实体模型的类库或控制台 Scaffold-DbContext  "数据库连接字符串" Microsoft.E ...

  2. 【译】第10节---数据注解-Key

    原文:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.asp ...

  3. 【Mysql】key 、primary key 、unique key 与index区别

    参考:https://blog.csdn.net/nanamasuda/article/details/52543177 总的来说,primary key .unique key 这些key建立的同时 ...

  4. Jedis使用

    Jedis是java调用Redis的接口. 一.Maven中的jedis依赖包 <!-- https://mvnrepository.com/artifact/redis.clients/jed ...

  5. python 安装pip

    https://pip.pypa.io/en/stable/installing/

  6. [原][osgEarth]添加自由飞行漫游器

    //头文件里 #define MANIPULATOR_W 0x01#define MANIPULATOR_A 0x02#define MANIPULATOR_S 0x04#define MANIPUL ...

  7. HDUOJ 不容易系列之(4)——考新郎

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2049 一开始我的想法就是使用错排公式,先使用全排列从N对中选出M对,然后再使用错排对选出的M对进行错排计 ...

  8. 利用vue-cli3快速搭建vue项目详细过程

    一.介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统.有三个组件: CLI:@vue/cli 全局安装的 npm 包,提供了终端里的vue命令(如:vue create .vue ...

  9. VUE项目实现页面跳转

    打开一个VUE项目,目录结构是这样的: 如现在有两个页面aaa和HelloWorld,路由配置在index.js中: import Vue from 'vue' import Router from ...

  10. 学习笔记37—WIN7系统本地连接没有有效的IP地址 电脑本地连接无有效ip配置怎么办

    WIN7系统本地连接没有有效的IP地址 电脑本地连接无有效ip配置怎么办 家中有两台笔记本都有无线网卡,现在想让两台笔记本都能够上网,而又不想购买路由器,交换机等设备,这个时候怎么办呢? 其实只要进行 ...