array(11): next permutation

描述:
31. Next Permutation
Total Accepted: 54346 Total Submissions: 212155 Difficulty: Medium
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

STL to do, spike

[html] view plain copy
class Solution {  
public:  
    void nextPermutation(vector<int>& nums) {  
        next_permutation(nums.begin(), nums.end());  
    }  
};  

This question does not feel very interesting, it is not easy to quickly come up with the rules:

But the more obvious rules are:

1, ascending order is the smallest combination, descending order is the largest combination

2. The next number of a certain number is the smallest combination number larger than him, such as 123, the next smallest combination larger than him is 132, which must be processed from the lower order

3. The processing of stl is to start from the low position (the end of the array),

a) Find the first adjacent ascending sequence, swap the previous data with the number larger than the first time (then invert the following number), for example: 123543 is 124533 (the first ascending sequence is 3, 5, but 4 is the first greater than 3 If it is larger, then swap it), the meaning of the operation here is to make the number larger through the first ascending order.

b) Obviously this number is not the smallest number greater than the original number, and the following number is the smallest after inversion, that is, 124533 is 124335. so, done! .

[cpp] view plain copy
//思路首先(不调用库函数):  
//举例:abc,acb,bac,bca,cab,cba  
class Solution {  
public:  
    void nextPermutation(vector<int>& nums) {  
        if(nums.empty() || nums.size()==1)  
            return;  
        vector<int>::iterator ite1=nums.end();  
        ite1--;  
        while(true)  
        {  
            vector<int>::iterator ite2=ite1;  
            ite1--;//ite1始终在ite2前面一个位置(左边算作最前面)  
            if(*ite1 < *ite2)//升序已经出现,接着重新从后面找首个升序位置  
            {  
                vector<int>::iterator itej=nums.end();  
                while(!(*ite1 < *--itej));  
                iter_swap(ite1,itej);//找到首个升序序列将其交换  
                reverse(ite2,nums.end());  
                return;  
            }  

            if(ite1==nums.begin())  
            {   
               reverse(nums.begin(),nums.end());  
               return;  
            }  
        }  
    }  
};  

Note: This blog post is original by EbowTang and may continue to be updated in the future. If reprinting, please be sure to copy this information!
Original address: http://blog.csdn.net/ebowtang/article/details/50450861
Original author blog: http://blog.csdn.net/ebowtang

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326432178&siteId=291194637