31. Next Permutation (Array)

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

思路:Array是无序的,但有规律可循。要交换的两个数,左边的那个总是比右边数小的数字中的最低位,右边的数是左边数右边的数字中比左边数大的最小的一个数;交换后递增排序左边数之后的数字

class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        int size = nums.size();
        if(size <= 1) return;

        int tmp, swapIndex = size;

        for(int lIndex = size-2; lIndex >= 0 ; lIndex--){ // to find the digit in the most right place whose value is smaller than a digit on its right
            for(int rIndex = lIndex+1; rIndex <size; rIndex++){ // to find the minimum digit who is larger than nums[lIndex]
                if(nums[lIndex] >= nums[rIndex]) continue;

                if(swapIndex == size){ //first find an eligible swap item
                    swapIndex = rIndex;
                }
                else if(nums[rIndex] < nums[swapIndex]){
                    swapIndex = rIndex;
                }
            }
            if(swapIndex != size){
                tmp = nums[lIndex];
                nums[lIndex] = nums[swapIndex];
                nums[swapIndex] = tmp;

                sort(nums.begin()+lIndex+1, nums.end());
                return;
            }
        }
        sort(nums.begin(), nums.end());
    }
};
时间: 2024-08-09 09:32:41

31. Next Permutation (Array)的相关文章

[array] leetcode - 31. Next Permutation - Medium

leetcode - 31. Next Permutation - Medium descrition 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

刷题31. Next Permutation

一.题目说明 题目是31. Next Permutation,英文太差看不懂,翻译了一下.才知道是求字典顺序下的下一个排列,不允许使用额外空间.题目难度是Medium! 二.我的实现 首先要进一步理解题目,以1->2->3为例,字典顺序如下: (1) 1->2->3; (2) 1->3->2; (3) 2->1->3; (4) 2->3->1; (5) 3->1->2; (6) 3->2->1; (7) 1->2-&

LeetCode 31. Next Permutation (下一个排列)

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 replaceme

LeetCode 31. Next Permutation

Problem: https://leetcode.com/problems/next-permutation/ 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 poss

[Leetcode][Python]31: Next Permutation

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 31: Next Permutationhttps://oj.leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If su

LeetCode OJ 31. Next Permutation

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 replaceme

31. Next Permutation

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 replaceme

31. Next Permutation(C++)

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 replaceme

[LeetCode] 31. Next Permutation ☆☆☆

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 replaceme