[Leetcode] The next sequence of leetcode31 code and problem-solving ideas

Title description

To achieve the function of getting the next permutation, the algorithm needs to rearrange the given sequence of numbers into the next larger permutation in the lexicographic order.

If there is no next larger arrangement, then rearrange the numbers to the smallest arrangement (that is, ascending order).

It must be modified in-situ, only extra constant space is allowed.

Here are some examples. The input is in the left column and the corresponding output is in the right column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

Problem solving ideas

No algorithm has been tested for this topic. It should be to examine the use of vector and thinking about the topic. As long as you understand the topic and think clearly, this topic is easy to do. I understand the title for a long time before I understand its meaning. In fact, in simple terms, take the example of 1, 2, and 3 given by the title as an example. What we want to get is three digits composed of the three numbers 1, 2, and 3. In the number, the next three digits greater than 123 is naturally 132. Because the other arrangements 213,231,312,321 are all greater than 132. So by writing a few examples on paper, we will find a rule. We start from nums[1] and keep judging nums[2], nums[3], so that we can find such a turning point index, which is the number of turning points. All numbers on the right are decreasing (not all numbers on the left are increasing). In examples 1,1,5, the turning point is nums[1], which is 1. At this time, we only need to find the smallest number next to the right of index that is greater than the value of index position , then we can exchange index and next numbers, and for All numbers from index+1 to nums.size()-1 are sorted in ascending order, and we can finally get the next arrangement we require.
At the beginning, when I submitted for the first time, dozens of samples did not pass, because I exchanged the two numbers index and index+1 directly at the beginning, and did not consider that there may be more than index+1 after index+1. The number is small and a value greater than the number of index. What we are looking for is the next permutation. Naturally, we should find the smallest number on the right side of the index that is greater than the value of the index position to exchange to ensure that it is the next permutation. Modified here, all the samples passed.

Passed code

class Solution {
    
    
public:
    void nextPermutation(vector<int>& nums) {
    
    
        int index=nums.size()-1;
        int next;
        int mid;
        for(int i=nums.size()-2;i>=0;i--)
        {
    
    
            if(nums[i]<nums[i+1])
            {
    
    
            	index=i;
            	break;
			}
        }
        if(index==(nums.size()-1))
        sort(nums.begin(),nums.end());
        else
        {
    
    
            next=index+1;
            while((next+1)<nums.size()&&nums[next+1]>nums[index])
            next++;
        	mid=nums[index];
	        nums[index]=nums[next];
	        nums[next]=mid;
	        sort(nums.begin()+index+1,nums.end());
		}
    }
};

Guess you like

Origin blog.csdn.net/qq_38391210/article/details/108295118