Leetcode question | Next Permutation

Source: Internet
Author: User

Leetcode question | Next Permutation

Problem:

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

 

Hide Tags Array

 

 

 

 

Question:

For a sequence, find the next sequence next to it. For example, the next sequence of 1, 2, and 3 is 1, 3, and 2;

Note: The next sequence of 3, 2, 1 is its inversion: 1, 2, 3

 

 

Thinking:

(1) This is an implementation method of the permutation and combination algorithm. The difficulty is to find the next sequence next to the sequence.

(2) STL is used here:

[STL] the principle and essence of next_permutation are to compare the back-end reverse sequence of a sequence. For example, the next sequence of 1, 2, and 3 is 1, 3, and 2. In this way, you only need to exchange 2 and 3. For sequences 1, 2, 5, 4, 3: Search for 2, 5 in reverse order. If the two elements are ordered, search for a number greater than 2 in reverse order, here is 3, and 2 Switch location, change to 1, 3, 5, 4, 2 and finally, reverse 5, 4, 2 to get the next sequence: 1, 3, 2, 4, 5

 

Code:

 

Class Solution {public: void nextPermutation (vector
 
  
& Num) {vector
  
   
: Iterator first = num. begin (); vector
   
    
: Iterator last = num. end (); vector
    
     
: Iterator tmp = first; if (+ + tmp = last) return; vector
     
      
: Iterator I = last; I --; for (;) {vector
      
        : Iterator ii = I; -- I;/** compares two adjacent elements in reverse order from the tail. If m elements in the tail are always in reverse order, it indicates that the tail is large enough * at this time, we need to constantly move to the header until we find a pair of adjacent elements I, ii, find an element j * that is smaller than I at the end, first swap the I and j elements, and then change ~ Element inversion between last elements */if (* I <* ii) {vector
       
         : Iterator j = last; while (! (* I <* -- j); iter_swap (I, j); // slightly different from swap (), iter_swap () the reverse (ii, last); return;} if (I = first) {reverse (first, last); // All reverse, it is the smallest dictionary sequence, for example, the CBA becomes abc return;} // }};
       
      
     
    
   
  
 


 

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.