Leetcode -- Next Permutation

Source: Internet
Author: User

Leetcode -- Next Permutation

Problem Description:

 

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,31,3,2
3,2,11,2,3
1,1,51,5,1

Analysis: the question refers to finding the next arrangement in the Lexicographic Order. We found the following rules:

We know that the weights of numbers increase from single digits. Let's look at the 154763,4 and 6 values of 154763. Start from a single digit. 6 is the first digit that is greater than 4, and the number of digits in 4 is greater than 6. If the two digits are exchanged, the total value increases.

The following figure shows the policy for finding the next arrangement:

1. Start searching from a single position and find the value of the first reverse order. In 154763, It is 4.

2. Start from a single position and find the first number that is greater than the reverse value. Here is 6.

3. The two numbers will get 156743 at last. We found that 156743 is not what we want, because 156743 is bigger than 156347.

4. So our last step is to sort 743, which is arranged at a minimum of 347.

5. In special cases, for example, if the initial number is in full reverse order, for example, 765431, the next value is 134567.

Write the following code according to this rule:
class Solution {public:    void swapp(int *a,int *b){    *a^=*b;    *b^=*a;    *a^=*b;}void nextPermutation(vector
 
   &num) {        int len=num.size();        int i,j;        if(len==0)            return;        for(i=len-2;i>=0;i--)            if(num[i]
  
   i;j--)                if(num[j]>num[i])                    break;            swapp(&num[i],&num[j]);            for(int s=i+1,p=len-1;s
   
    

 

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.