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. JavaScript 调试常见报错以及原因

    JavaScript 调试常见报错以及原因 测试环境 chrome 版本 66.0.3359.170(正式版本) (64 位) TypeError 类型错误 不是操作符所接受的数据类型. //---- ...

  2. 2-4、nginx特性及基础概念-nginx web服务配置详解

    Nginx Nginx:engine X 调用了libevent:高性能的网络库 epoll():基于事件驱动event的网络库文件 Nginx的特性: 模块化设计.较好扩展性(不支持模块动态装卸载, ...

  3. 创建react项目

    npm搭建React项目 React官网提供最简便的方法是使用create-react-app npx create-react-app my-app cd my-app npm start 也可以自 ...

  4. Jenkins参数化构建(三)之 Jenkins从文件中读取运行参数

    安装Extended Choice Parameter插件 选择‘参数化构建过程’ maven command line中进行引用 clean test -DsuiteXmlFile=src/main ...

  5. git服务器搭建全程

    为了后续安装能正常进行,我们先来安装一些相关依赖库和编译工具 [root@VM_95_113_centos ~]# yum install curl-devel expat-devel gettext ...

  6. springboot学习之授权Spring Security

    SpringSecurity核心功能:认证.授权.攻击防护(防止伪造身份) 涉及的依赖如下: <dependency> <groupId>org.springframework ...

  7. Ubuntu 下 Python自由切换

    sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 sudo update-alternati ...

  8. PCA-主成分分析(Principal components analysis)

    来自:刘建平 主成分分析(Principal components analysis,以下简称PCA)是最重要的降维方法之一. 1. PCA的思想 PCA顾名思义,就是找出数据里最主要的方面,用数据里 ...

  9. java反编译器

    一时手残,把java工程中的源文件给删了,幸亏还有.class文件,想起java可以反编译,所以试一试. JD-Eclipse 如果是使用Eclipse的话,可以用Eclipse插件JadClipse ...

  10. vue init webpack nameXXX 报错问题:

    vue新建demo项目报错如下: M:\lhhVueTest>vue init webpack L21_VueProject vue-cli · Failed to download repo ...