31. Next Permutation

permutation of an array of integers is an arrangement of its members into a sequence or linear order.

  • For example, for arr = [1,2,3], the following are considered permutations of arr[1,2,3][1,3,2][3,1,2][2,3,1].

The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).

  • For example, the next permutation of arr = [1,2,3] is [1,3,2].
  • Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
  • While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.

Given an array of integers numsfind the next permutation of nums.

The replacement must be in place and use only constant extra memory.

Example 1:

Input: nums = [1,2,3]
Output: [1,3,2]

Example 2:

Input: nums = [3,2,1]
Output: [1,2,3]

Example 3:

Input: nums = [1,1,5]
Output: [1,5,1]

Constraints:

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 100

题目:给定一个数组,找下一个排列。

思路:从排列来看,例:122345->122354->122435->122453->123245->123254->123425->123452->...数组是逐渐从正序排列122345变化到543221的。每次将其中一个位逐渐变大(如果后面有比它大的数值的话)。因此需要从后面开始找到第一个降序的数值位置i,与后面比nums[i]值大的最小数值位置,注意,一定是比nums[i]大的最小值,例如[1,2,5,4,3,2]的下一个排列,需要从后面开始找nums[5]=2 < nums[4]=3 < nums[3]=4 < nums[2]=5 > nums[1]=2。因此找到第一个降序的数值nums[1]=2,再往后找比2大的最小值,找到nums[4]=3,将nums[1]和nums[4]交换,变为[1,3,5,4,2,2]。但这只能保证数组变大了,不能保证是“下一个”。还需要保证数组要是交换之后的最小值。因此保持nums[0~1]不变,将后面的子数组排序,使其从小到大。注意,由于交换之后后面的值肯定是从大到小排序的,因此用reverse也行,用sort也能达到目的

代码:

class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        int i = nums.size()-1;
        while(i > 0 && nums[i-1] >= nums[i]) i--;
        i--;
        if(i >= 0){
            int j = i+1;
            while(j < nums.size() && nums[j] > nums[i]) j++;
            j--;
            swap(nums[i], nums[j]);
        }
        //sort(nums.begin()+i+1, nums.end());
        reverse(nums.begin()+i+1, nums.end());
        return;
    }
};

time: O(N). space:O(1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值