LeetCode Medium: 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 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

字典序排序生成算法,大致意思就是给定一个序列,生成下一个比之大的序列,若已经是最大的,则返回最小的,例如:123-132-213-231-312-321(将123进行排列组合,保证下一个比前一个大,”下一个函数”需要输入排列组合,输出下一个) 

二、思路

1)由于下一个数比上一个数大,因此需要从后往前扫描,找到递增的位置设为元素i,j(i小于j) ,这里的往前寻找的起始位置为倒数第2个,i 是当前元素,这里的递增指的是第 i 个元素小于第 j 个元素;
2)由于下一个数虽然大于上一个数,且最接近上一个数,因此找到元素 i,在i元素后面找到最接近 i 且大于i的元素 k 。由于 i 后面的元素都是降序排列的,只需要从后往前扫描找到第一个比 i 大的元素即可 
3)找到将 i 和 k 交换位置,然后将 k 后面的元素递增排序 
4)找不到,则将全部元素递增排序

三、代码

#coding:utf-8
def nextPermutation(nums):
    """
    :type nums: List[int]
    :rtype: void Do not return anything, modify nums in-place instead.
    """
    if len(nums) <= 1:
        return
    for i in range(len(nums) - 2, -1, -1):
        if nums[i] < nums[i + 1]:
            for k in range(len(nums) - 1, i, -1):
                if nums[k] > nums[i]:
                    nums[i], nums[k] = nums[k], nums[i]
                    nums[i + 1:] = sorted(nums[i + 1:])    #对i后面的元素进行排序
                    break
            break
        else:
            if i == 0:
                nums.sort()
    print(nums)
    return nums
if __name__ == ‘__main__‘:
    nums = [2,7,6,3,5,4,1]
    nextPermutation(nums)

  参考博客:https://blog.csdn.net/qq_28119401/article/details/52972616    https://www.cnblogs.com/mydesky2012/p/5620006.html   https://blog.csdn.net/nomasp/article/details/49913627  https://www.cnblogs.com/zhang-hill/p/5067057.html  https://blog.csdn.net/ljiabin/article/details/44943881

原文地址:https://www.cnblogs.com/xiaodongsuibi/p/8970491.html

时间: 2024-11-09 02:50:16

LeetCode Medium: 31. Next Permutation的相关文章

[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

【一天一道LeetCode】#31. Next Permutation

一天一道LeetCode系列 (一)题目 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

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

[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] Subsets [31]

题目 Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3]

乘风破浪:LeetCode真题_031_Next Permutation

乘风破浪:LeetCode真题_031_Next Permutation 一.前言 这是一道经典的题目,我们实在想不出最好的方法,只能按照已有的方法来解决,同时我们也应该思考一下为什么要这样做?是怎么想到的?这比我们记住步骤更加的有用. 二.Next Permutation 2.1 问题 2.2 分析与解决 排列(Arrangement),简单讲是从N个不同元素中取出M个,按照一定顺序排成一列,通常用A(M,N)表示.当M=N时,称为全排列(Permutation).从数学角度讲,全排列的个数A

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