Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place and use only constant extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

这道题让我们求下一个排列顺序,由题目中给的例子可以看出来,如果给定数组是降序,则说明是全排列的最后一种情况,则下一个排列就是最初始情况,可以参见之前的博客 Permutations。再来看下面一个例子,有如下的一个数组

1  2  7  4  3  1

下一个排列为:

1  3  1  2  4  7

那么是如何得到的呢,我们通过观察原数组可以发现,如果从末尾往前看,数字逐渐变大,到了2时才减小的,然后再从后往前找第一个比2大的数字,是3,那么我们交换2和3,再把此时3后面的所有数字转置一下即可,步骤如下:

1    7  4  3  1

1    7  4    1

1    7  4    1

1  3  1  2  4  7

解法一:

class Solution {
public:
void nextPermutation(vector<int> &num) {
int i, j, n = num.size();
for (i = n - ; i >= ; --i) {
if (num[i + ] > num[i]) {
for (j = n - ; j > i; --j) {
if (num[j] > num[i]) break;
}
swap(num[i], num[j]);
reverse(num.begin() + i + , num.end());
return;
}
}
reverse(num.begin(), num.end());
}
};

下面这种写法更简洁一些,但是整体思路和上面的解法没有什么区别,参见代码如下:

解法二:

class Solution {
public:
void nextPermutation(vector<int>& nums) {int n = nums.size(), i = n - , j = n - ;
while (i >= && nums[i] >= nums[i + ]) --i;
if (i >= ) {
while (nums[j] <= nums[i]) --j;
swap(nums[i], nums[j]);
}
reverse(nums.begin() + i + , nums.end());
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/31

类似题目:

Permutations II

Permutations

Permutation Sequence

Palindrome Permutation II

Palindrome Permutation

参考资料:

https://leetcode.com/problems/next-permutation/

https://leetcode.com/problems/next-permutation/discuss/13921/1-4-11-lines-C%2B%2B

https://leetcode.com/problems/next-permutation/discuss/13867/C%2B%2B-from-Wikipedia

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 31. Next Permutation 下一个排列的更多相关文章

  1. [leetcode]31. Next Permutation下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  2. leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

    Next Permutation  Implement next permutation, which rearranges numbers into the lexicographically ne ...

  3. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  4. 【LeetCode每天一题】Next Permutation(下一个排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  5. LeetCode(31): 下一个排列

    Medium! 题目描述: (请仔细读题) 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列) ...

  6. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  7. 031 Next Permutation 下一个排列

    实现获取下一个排列函数,这个算法需要将数字重新排列成字典序中数字更大的排列.如果不存在更大的排列,则重新将数字排列成最小的排列(即升序排列).修改必须是原地的,不开辟额外的内存空间.这是一些例子,输入 ...

  8. lintcode:next permutation下一个排列

    题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...

  9. 31. Next Permutation (下一个全排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

随机推荐

  1. java基于NIO的分散读取文件,然后统一聚合后写入文件

    分散读取:对于一个文件,可以分散的读取数据,可以快速的读取,好比多个线程在分段同时读取: 聚合写入:为了提高效率,一般读取到的数据都是分散的,要快速写入,就需要把分散的数据聚集在一起,然后一块写入到文 ...

  2. DEDECMS安全设置怎样做,您就不会说它不安全了

    dedecms是好用,优化也好,就是不安全,个人是这样认为的,今天 闲着没事 整理了一些有助于dedecms网站安全的一些设置,可以说是目前最全的dedecms安全设置! 其一:保持DEDE更新,及时 ...

  3. 纯C语言实现链栈

    #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct StackNode{ E ...

  4. 五分钟看懂UML类图与类的关系详解

    在画类图的时候,理清类和类之间的关系是重点.类的关系有泛化(Generalization).实现(Realization).依赖(Dependency)和关联(Association).其中关联又分为 ...

  5. linux桌面系统的约定

    linux系统的桌面系统基本遵循同样的约定. mime类型 在linux下,关于文件类型的信息通常放在/usr/share/mime./usr/local/share/mime和用户目录下,所有应用程 ...

  6. 设计模式之设计原则 C#

    成为一名资深架构师首先要懂设计模式,在懂之前,要清楚设计原则,原来我就吃过这个亏,很久以前有人问我设计原则,我是一头茫然,不是只有设计模式吗?且不知设计原则就像是写书法一样,楷体就是方正,竖道有垂露等 ...

  7. 高性能TcpServer(C#) - 4.文件通道(处理:文件分包,支持断点续传)

    高性能TcpServer(C#) - 1.网络通信协议 高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP) 高性能TcpS ...

  8. Jmeter-While控制器

    Jmeter-While控制器 背景: 用作循环的控制器里, 最常用的就’循环控制器’和’While控制器’. 循环控制器仅能定义循环次数(永远和自定义次数). 缺乏判断能力.While控制器正好弥补 ...

  9. Mysql时区无法识别

    Unable to connect to database. Tried 1 times {:error_message=>“Java::JavaSql::SQLException: The s ...

  10. jenkins使用小技巧:jenkins构建出来的war/jar包如何带上SVN号?

    在实际使用过程中,一般会这样比如说打出来的包是 mypackage.jar, 但是每次打出来都是固定的 mypackage.jar如何来区分和上一个包的区别呢? 一般来说,会把打出来的包带上个 svn ...