LeetCode: Next Permutation [030]

【题目】

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, do not allocate 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

【题意】

给定一组数的一个排列,求在全排中该排列的下一个排列是多少。

如果给定的恰好是最后一个排列,则返回第一个排列。

【思路】

举几个例子:

prev                    next

2,    3                   =>  3,    2

1,    3, 2               =>  2,    1, 3

2,    4, 3, 1           =>  3,    1, 2, 3

4,    5, 4, 3, 2       =>  5,    2, 3, 4, 4

上面的例子中第一个和第二个数之间刻意加了一个比较大的间隔。仔细观察一下发现,前一个排列从第二数开始值依次递减,而其next排列从第二数开始依次递增。next排列的第一个数是前一个排列的第一个数在升序排列中的后一个数【即第一个比它大的数】

有了上面的观察,本题的方法就是要在排列的尾部从后向前找一个符合上面prev排列形式的子排列。即第一个数比第二个数小,第二个数开始递。

然后,next排列生成策略为:取排列中比第一个数略大的那个数放到第一位,其他数升序排列。

【代码】

class Solution {
public:
    void nextPermutation(vector<int> &num) {
        int size=num.size();
        int i=size-2;
        while(i>=0 && num[i]>=num[i+1])i--;
        if(i<0)sort(num.begin(), num.end());
        else{
            int j=i+1;    //找升序排列中第一个大于num[i]的数
            while(j<size&&num[j]>num[i])j++;
            j--;
            int temp=num[i];
            num[i]=num[j];
            num[j]=temp;
            sort(num.begin()+i+1,num.end()); //除第一个数外,顺序排列
        }
    }
};

LeetCode: Next Permutation [030]

时间: 2024-12-21 13:44:54

LeetCode: Next Permutation [030]的相关文章

Leetcode:Next Permutation 下一个排列

Next Permutation: 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 ord

LeetCode OJ--Next Permutation *

求一个排列的下一个排列. 1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1 #include <iostream> #include <vector> #include <algorithm> using namespace std; class Solution{ public: void nextPermutation(vector<int> &num) { if(num.size() == 0) return; c

【leetcode】 Permutation Sequence

问题: 对于给定序列1...n,permutations共有 n!个,那么任意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小开始直接到k,估计会超时,受10进制转换为二进制的启发,对于排列,比如 1,2,3 是第一个,那么3!= 6,所以第6个就是3,2,1.也就是说,从开始的最小的序列开始,到最大的序列,就是序列个数的阶乘数.那么在1,3 , 2的时候呢?调整一下,变成2,1,3,就可以继续. 实现: int getFactorial(int n

Leetcode Palindrome Permutation

Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even length. What difference d

[LeetCode] Find Permutation 找全排列

By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decreasing relationship between two numbers, 'I' represents an increasing relationship between two numbers. And our secret signature was constructed by a s

LeetCode——Next Permutation

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 replaceme

[leetcode]Next Permutation @ Python

原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: 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 p

LeetCode Next Permutation

原题链接在这里:https://leetcode.com/problems/next-permutation/ 参考了这篇博文:http://blog.csdn.net/linhuanmars/article/details/20434115 分两种情况: 1. 从前往后一路数值一直变小,如"654321", 返回的应该是"123456"也就是整个反转 2. 数值没有一直变下,如"236541", 此时从后往前找到第一个数值下降的点3<6,

Leetcode 60. Permutation Sequence

The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): Given n and k, return the kth permutation sequence. Note: Given n will be between