[C + +] Leetcode:79 next permutation (next arrangement, common interview questions)

Source: Internet
Author: User

Topic:

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

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

The replacement must is in-place, do not allocate extra memory.

Here is some examples. Inputs is in the left-hand column and its corresponding outputs is in the right-hand column.
1,2,31,3,2
3,2,11,2,3
1,1,51,5,1

idea: the so-called next permutation of a permutation means that there is no other arrangement between this arrangement and the next arrangement. This requires that we have a common prefix as long as possible for this arrangement and the next arrangement, that is, the change is limited to the shortest possible suffix.

As an example:

such as sequence, 7 8 6 9 8 7 2; 7 8 7 9 8 6 2; 7 8 7 2 6 8 9;

first Look at the front 2 rows, you can see that the second row is larger than the first row, the reference string comparison size problem. So the 2nd permutation is not the next permutation of the first arrangement. Obviously not, the 3rd permutation is, then how to get to the next arrangement. Simple steps: Assuming an array size of n1. From the back forward, find the first a[i-1] < A[i]. That is, the 6 position in the first permutation, you can see a[i] to a[n-1] These are monotonically decreasing sequences. 9,8,7,2 Descending2. From a[n-1] to A[i], find a value larger than a[i-1] (that is, the value in a[n-1] to a[i) to find the smallest value in a collection larger than a[i-1]. The first element greater than I is found at the end and is recorded as J. That is, 7. 3. Swap these two values and sort a[n-1] to a[i] from small to large. Since j is the first element greater than I, after the interchange, it still satisfies the descending arrangement, and the direct reverse gets the ascending order. 8 6 4 3 2 rearrange in increments. 4. If an arrangement has no larger next arrangement (i.e. the permutation is ascending order), flip the entire arrangement and get the smallest arrangement.
Attention: 1. After locating to I, need to find the first element greater than I j, equal to neither, must satisfy greater than. (equals swap meaningless)
Find the first number greater than num[i-1] after i-1, and swap                int j = n-1;                while (Num[i] >= num[j]) j--;                Swap (Num[i], num[j]);
2. Since j is the first element greater than I, after the Exchange, II starts to the end, still satisfies the descending order, the direct reverse gets the increment arrangement. Note that the flip starts from II.
After the exchange, it is still in descending order. Reverse (Num.begin () + II, Num.end ());
3. Simple memory steps
    • Looking forward from the end of the end to two adjacent elements, both satisfying I < II (make the first element I, the second element is II)
    • If no such pair of elements are found, the current arrangement is the largest and there is no next large arrangement
    • If found, then start at the end to find the first element greater than I, recorded as J
    • Swap element I, j, and then reverse all elements behind II (including II)
    • If an arrangement has no larger next arrangement (that is, the permutation is descending order), calling this function flips the original arrangement, resulting in a minimal arrangement
Complexity: O (N) AC Code:
Class Solution {public:    void Nextpermutation (vector<int> &num) {        int n = num.size ();        if (n = = 1) return;                for (int i = n-2, ii = n-1; I >= 0; I--, ii--)        {            //Find first group of adjacent descending combinations            if (Num[i] < Num[ii])            {                //Find I-1 Then the first number is greater than num[i-1], and the interchange                int j = n-1;                while (Num[i] >= num[j]) j--;                Swap (Num[i], num[j]);                After the exchange, it is still in descending order.                Reverse (Num.begin () + II, Num.end ());                return;            }        }        If the permutation is descending order, flip the entire array        reverse (Num.begin (), Num.end ());        return;}    };




[c++]leetcode:79 next permutation (next arrangement, common interview questions)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.