[牛客网-Leetcode] #Array Medium next-permutation

The next full array next-permutation

Title description

Implementation function next permutation (next permutation): rearrange the numbers in the permutation to the next larger permutation in lexicographic order. Rearrange the numbers in the permutation to the next larger permutation in lexicographic order.
If there is no such arrangement, arrange it as the least lexicographic arrangement (ascending order). In-
situ algorithm is needed to solve this problem. You cannot apply for additional memory space.
The following organic group samples, the left is the input data, and the right is Is the output answer
1,2,3→1,3,2
3,2,1→1,2,3
1,1,5→1,5,1

Implement next permutation, which rearranges numbers into the lex
icographically 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

Problem-solving ideas

  • Idea 1 : Use the next_permutation function under the algorithm header file to rearrange the sequence into the next full permutation.
  • Note that you need to add an if judgment. The next_permutation function will return false when it has reached the last full permutation . At this time, the title requires the arrangement to be the smallest lexicographical arrangement, so sort by sort
class Solution {
    
    
public:
    void nextPermutation(vector<int> &num) {
    
    
        if(num.size() == 0) return ;
        //如果存在下一个全排列,则返回true,否则返回false
        if(next_permutation(num.begin(), num.end())) {
    
    
            
        } else {
    
    
            sort(num.begin(), num.end());
        }
    }
};
  • Idea 2 : Find from the back to the frontAscending sortThe subscript of the last point, swap the element before the point and the first after itmore than theThe number of this element, and then sort the following parts in ascending order
    • For example: 1 2 3 7 6 5 1
    • Find the index of the last point in ascending order from back to front
    • If there is no such point, it proves that the entire array is decreasing and has reached the largest full permutation, so you can directly reverse the entire array
    • If there is such a point, such as 7 in the above example, then look for the first number greater than 3 from back to front. In this example, it is 5
    • Swap the positions of 5 and 3, and arrange the following part of the array in ascending order to get the result.
class Solution {
    
    
public:
    void nextPermutation(vector<int> &num) {
    
    
        int n = num.size();
        if(n <= 1) return ;
        //标记从后往前递增排序最后一个点的下标
        int index(n - 1);
        //跳出循环说明是num[index] > num[index - 1],即严格递增
        while(index > 0 && num[index] <= num[index - 1]) index --;
        
        if(index == 0) {
    
      //如果不存在这样的点
            reverse(num.begin(), num.end());
        } else {
    
         //如果存在这样的点
            //标记第一个大于这个点前一个元素的数
            int k(n - 1);
            //跳出循环说明是num[k] > num[index - 1],即严格大于
            while(num[k] <= num[index - 1]) k --;
            swap(num[k], num[index - 1]);
            sort(num.begin() + index, num.end());
        }
    }
};

Guess you like

Origin blog.csdn.net/cys975900334/article/details/106565004