Leetcode31. Next Permutation

Leetcode31. 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 replacement must be in-place and use only constant 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
解题

设P是1~n的一个全排列: p = p 1 p 2 . . . . . . p n = p 1 p 2 . . . . . . p j − 1 p j p j + 1 . . . . . . p k − 1 p k p k + 1 . . . . . . p n p=p_1p_2......p_n=p_1p_2......p_{j-1}p_{j}p_{j+1}......p_{k-1}p_kp_{k+1}......p_{n} p=p1p2......pn=p1p2......pj1pjpj+1......pk1pkpk+1......pn

  1. 从排列的右端开始,找出第一个比右边相邻数字小的数字的序号j(j从左端开始计算),即 j = m a x i ∣ p i < p i + 1 j=max{i|p_i<p_i+1} j=maxipi<pi+1
  2. p j p_j pj的右边的数字中,找出所有比p_j大的数中最小的数字p_k,即 k=max{i|p_i>p_j}(右边的数从右至左是递增的,因此k是所有大于pj的数字中序号最大者)
  3. 对换 p i p_i pi p k p_k pk
  4. 再将 p j + 1 . . . . . . p k − 1 p k p k + 1 . . . . . . p n p_{j+1}......p_{k-1}p_kp_{k+1}......p_n pj+1......pk1pkpk+1......pn倒转得到排列 p ′ = p 1 p 2 . . . . . p j − 1 p j p n . . . . . p k + 1 p k p k − 1 . . . . . p j + 1 p'=p_1p_2.....p_{j-1}p_jp_n.....p_{k+1}p_kp_{k-1}.....p_{j+1} p=p1p2.....pj1pjpn.....pk+1pkpk1.....pj+1,这就是排列p的下一个排列。
比如 nums = [1,2,7,4,3,1],下一个排列是什么?
我们找到第一个最大索引是 nums[1] = 2
再找到第二个最大索引是 nums[4] = 3
交换,nums = [1,3,7,4,2,1];
翻转,nums = [1,3,1,2,4,7].

在这里插入图片描述

  • 时间复杂度:O(n),在最坏的情况下,只需要对整个数组进行两次扫描。
  • 空间复杂度:O(1),没有使用额外的空间,原地替换足以做到。
Java
public class Solution {
    public void nextPermutation(int[] nums) {
        int i = nums.length - 2;//注意i的初始值
        while (i >= 0 && nums[i + 1] <= nums[i]) {
            i--;//从右往左找到比右相邻小的
        }
        if (i >= 0) {
            int j = nums.length - 1;//注意j的初始值
            while (j >= 0 && nums[j] <= nums[i]) {
                j--;//从右往左找到第一个比nums[i]大的数nums[j]
            }
            swap(nums, i, j);//交换i,j
        }
        reverse(nums, i + 1);//交换[i+1,nums.length - 1]
    }

    private void reverse(int[] nums, int start) {//
        int i = start, j = nums.length - 1;
        while (i < j) {
            swap(nums, i, j);
            i++;
            j--;
        }
    }
    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
Python
def nextPermutation(self, nums):
    i = j = len(nums)-1
    while i > 0 and nums[i-1] >= nums[i]:
        i -= 1
    if i == 0:   # nums are in descending order
        nums.reverse()
        return 
    k = i - 1    # find the last "ascending" position
    while nums[j] <= nums[k]:
        j -= 1
    nums[k], nums[j] = nums[j], nums[k]  
    l, r = k+1, len(nums)-1  # reverse the second part
    while l < r:
        nums[l], nums[r] = nums[r], nums[l]
        l +=1 ; r -= 1
C++
  1. Find the largest index k such that nums[k] < nums[k + 1]. If no such index exists, just reverse nums and done.
  2. Find the largest index l > k such that nums[k] < nums[l].
  3. Swap nums[k] and nums[l].
  4. Reverse the sub-array nums[k + 1:].
class Solution {
public:
    void nextPermutation(vector<int>& nums) {
    	int n = nums.size(), k, l;
    	for (k = n - 2; k >= 0; k--) {
            if (nums[k] < nums[k + 1]) {
                break;
            }
        }
    	if (k < 0) {
    	    reverse(nums.begin(), nums.end());
    	} else {
    	    for (l = n - 1; l > k; l--) {
                if (nums[l] > nums[k]) {
                    break;
                }
            } 
    	    swap(nums[k], nums[l]);
    	    reverse(nums.begin() + k + 1, nums.end());
        }
    }
}; 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值