Middle-题目88:31. Next Permutation

原创 2016年05月31日 17:26:20

题目原文:
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
题目大意:
求出一个数组的下一个字典序的排序结果。
题目分析:
使用C++中的库函数next_permutation实现(在头文件algorithm中)。
函数接口:bool next_permutation( iterator start, iterator end);
即求start-end这个序列的下一个排序。
源码:(language:cpp)

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

成绩:
12ms,beats 10.04%,众数12ms,88.95%
Cmershen的碎碎念:
C++11中,Next_permutation的源码如下:

    bool _Next_permutation(_BidIt _First, _BidIt _Last, _Pr _Pred)
    {   // permute and test for pure ascending, using _Pred
    _BidIt _Next = _Last;
    if (_First == _Last || _First == --_Next)
        return (false);

    for (; ; )
        {   // find rightmost element smaller than successor
        _BidIt _Next1 = _Next;
        if (_DEBUG_LT_PRED(_Pred, *--_Next, *_Next1))
            {   // swap with rightmost element that's smaller, flip suffix
            _BidIt _Mid = _Last;
            for (; !_DEBUG_LT_PRED(_Pred, *_Next, *--_Mid); )
                ;
            _STD iter_swap(_Next, _Mid);
            _STD reverse(_Next1, _Last);
            return (true);
            }

        if (_Next == _First)
            {   // pure descending, flip all
            _STD reverse(_First, _Last);
            return (false);
            }
        }
    }

大致思路:
在当前序列中,从尾端向前寻找两个相邻元素,前一个记为*i,后一个记为*t,并且满足*i < *t。然后再从尾端寻找另一个元素*j,如果满足*i < *j,即将第i个元素与第j个元素对调,并将第t个元素之后(包括t)的所有元素颠倒排序,即求出下一个序列了。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

LeetCode 31 Next Permutation (C,C++,Java,Python)

Problem: Implement next permutation, which rearranges numbers into the lexicographically next g...

Leetcode——31. Next Permutation

题目Implement next permutation, which rearranges numbers into the lexicographically next greater permu...

【leetcode】【31】Next Permutation

一、问题描述 Implement next permutation, which rearranges numbers into the lexicographically next gre...

leetcode31~Next Permutation

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

【LeetCode】31. Next Permutation解法

31. Next Permutation Implement next permutation, which rearranges numbers into the lexicographic...

31. Next Permutation | Java最短代码实现

原题链接:31. Next Permutation 【思路】 首先,我们来了解一下——字典序法: C++的STL库里面有nextPermutation()方法,其实现就是字典序法。 下...

leetcode No31. Next Permutation

Question: Implement next permutation, which rearranges numbers into the lexicographically next grea...

LeetCode 31: Next Permutation

Next PermutationImplement next permutation, which rearranges numbers into the lexicographically next...

Leetcode 31 Next Permutation (C++实现)

Leetcode 31 Next Permutation解题。c++实现。

LeetCode (31)Next Permutation

(31)Next Permutation题目:找到比目标数大的,只使用目标数内数字组成的最小的数。如果这种安排是不可能的,它必须重新安排它作为最低可能的顺序(即按升序排序)。更换必须到位,不要分配额外...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目88:31. Next Permutation
举报原因:
原因补充:

(最多只允许输入30个字)