leetcode 之 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, 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,31,3,2
3,2,11,2,3
1,1,51,5,1

解释: 求下一个全排列。 认为全排列是按顺序依次排列,比如, 1, 2, 3 -》 1, 3, 2 -》 2, 1, 3 ,对于求全排列的问题,可以采用回溯法求解(注,面试时问道了这个问题,然后被灭了~)回溯法如图:

具体的回溯法求全排列,接下来再讲。这里继续讲下一个全排列的问题。

如上图, 对于1,3, 2来说,下一个全排列的求解,回溯至1,3,? 发现只有1,3, 2一种选择, 继续回溯至1,??,对于1,2 ? 一定是其前一个,而不是下一个,因此继续回溯,至 2,?,? 的第一个即 2, 1,3.

参考网上算法如下:

1. 从右往左,找到第一个非递增index的位置。如,1,3, 2 则找到 1.  如 7, 8, 6 ,9, 8, 7, 2 则找到 pos[2] = 6

2. 从右往左,找到第一个大于pos[index] 的数,1, 3, 2 即 为2.   7,8,6,9,8,7,2 即为 pos[5] = 7

3. 交换 index 和步骤2 找到的数。 1,3,2 -> 2, 3,1      7,8,6,9,8,7,2 -> 7,8,7,9,8,6,2

4. 将index后面的数据,翻转。 2,3,1-> 2, 1, 3     7,8,7,9,8,6,2-> 7,8,7,2, 6, 8, 9

即得到所需答案。代码如下:

class Solution(object):
    def nextPermutation(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        le = len(nums)
        index = -1

        for i in range(le-1, 0, -1):
            if nums[i] <= nums[i-1]:
                pass
            else:
                index = i-1
                break
        if index != -1:
            for i in range(le-1, index, -1):
                if nums[i] > nums[index]:
                    nums[i], nums[index] = nums[index], nums[i]
                    break
        i = index+1
        j = le-1
        while (i <= j):
            nums[i], nums[j] = nums[j], nums[i]
            i = i + 1
            j = j - 1
        
时间: 2024-10-14 08:14:43

leetcode 之 Next Permutation的相关文章

[LeetCode] 031. Next Permutation (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 031. Next Permutation (Medium) 链接: 题目:https://oj.leetcode.com/problems/next-permutation/ 代码(github):https://github.com/illuz/leetcode 题意: 求一个序列的下一个排列. 分析: 可以用

[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

[LeetCode] 267. Palindrome Permutation II 回文全排列 II

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s = "a

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 31.Next Permutation (下一个字典序排序) 解题思路和方法

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 orde

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][JavaScript]Next Permutation

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 orde

【LeetCode】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 repl

LeetCode 31 Next Permutation(下一个排列)

翻译 实现"下一个排列"函数,将排列中的数字重新排列成字典序中的下一个更大的排列. 如果这样的重新排列是不可能的,它必须重新排列为可能的最低顺序(即升序排序). 重排必须在原地,不分配额外的内存. 以下是一些示例,左侧是输入,右侧是输出: 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1 原文 Implement next permutation, which rearranges numbers into the lexicographically

[LeetCode][Java] 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 repla