【LeetCode】 31.下一个排列

简介: 31.下一个排列

31.下一个排列

力扣题目链接

实现获取 下一个排列 的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。

如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。

必须 原地 修改,只允许使用额外常数空间。

示例 1:

  • 输入:nums = [1,2,3]
  • 输出:[1,3,2]

示例 2:

  • 输入:nums = [3,2,1]
  • 输出:[1,2,3]

示例 3:

  • 输入:nums = [1,1,5]
  • 输出:[1,5,1]

示例 4:

  • 输入:nums = [1]
  • 输出:[1]

思路

一些同学可能手动写排列的顺序,都没有写对,那么写程序的话思路一定是有问题的了,我这里以1234为例子,把全排列都列出来。可以参考一下规律所在:

1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1

如图:

以求1243为例,流程如图:

31.下一个排列.png

对应的C++代码如下:

class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        for (int i = nums.size() - 1; i >= 0; i--) {
            for (int j = nums.size() - 1; j > i; j--) {
                if (nums[j] > nums[i]) {
                    swap(nums[j], nums[i]);
                    reverse(nums.begin() + i + 1, nums.end());
                    return;
                }
            }
        }
        // 到这里了说明整个数组都是倒序了,反转一下便可
        reverse(nums.begin(), nums.end());
    }
};

其他语言版本

Java

class Solution {
    public void nextPermutation(int[] nums) {
        for (int i = nums.length - 1; i >= 0; i--) {
            for (int j = nums.length - 1; j > i; j--) {
                if (nums[j] > nums[i]) {
                    // 交换
                    int temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                    // [i + 1, nums.length) 内元素升序排序
                    Arrays.sort(nums, i + 1, nums.length);
                    return;
                }
            }
        }
        Arrays.sort(nums); // 不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。
    }
}
优化时间复杂度为O(N),空间复杂度为O(1)
class Solution {
    public void nextPermutation(int[] nums) {
        // 1.从后向前获取逆序区域的前一位
        int index = findIndex(nums);
        // 判断数组是否处于最小组合状态
        if(index != 0){
            // 2.交换逆序区域刚好大于它的最小数字
            exchange(nums,index);
        }
        // 3.把原来的逆序区转为顺序
        reverse(nums,index);
    }
    
    public static int findIndex(int [] nums){
        for(int i = nums.length-1;i>0;i--){
            if(nums[i]>nums[i-1]){
                return i;
            }
        }
        return 0;
    }
    public static void exchange(int [] nums, int index){
        int head = nums[index-1];
        for(int i = nums.length-1;i>0;i--){
            if(head < nums[i]){
                nums[index-1] = nums[i];
                nums[i] = head;
                break;
            }
        }
    }
    public static void reverse(int [] nums, int index){
        for(int i = index,j = nums.length-1;i<j;i++,j--){
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }
    }   
}

Python

直接使用sorted()会开辟新的空间并返回一个新的list,故补充一个原地反转函数
class Solution:
    def nextPermutation(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        length = len(nums)
        for i in range(length - 2, -1, -1): # 从倒数第二个开始
            if nums[i]>=nums[i+1]: continue # 剪枝去重
            for j in range(length - 1, i, -1):
                if nums[j] > nums[i]:
                    nums[j], nums[i] = nums[i], nums[j]
                    self.reverse(nums, i + 1, length - 1)
                    return  
        self.reverse(nums, 0, length - 1)
    
    def reverse(self, nums: List[int], left: int, right: int) -> None:
        while left < right:
            nums[left], nums[right] = nums[right], nums[left]
            left += 1
            right -= 1
            
"""
265 / 265 个通过测试用例
状态:通过
执行用时: 36 ms
内存消耗: 14.9 MB
"""
目录
相关文章
|
7月前
【Leetcode -441.排列硬币 -448.找到所有数组中消失的数字】
【Leetcode -441.排列硬币 -448.找到所有数组中消失的数字】
26 0
|
7月前
|
程序员
【Leetcode】面试题 01.02. 判定是否互为字符重排、面试题 01.04. 回文排列
目录 面试题 01.02. 判定是否互为字符重排 面试题 01.04. 回文排列
36 0
|
3天前
|
Go
golang力扣leetcode 937.重新排列日志文件
golang力扣leetcode 937.重新排列日志文件
30 0
|
3天前
|
Go
golang力扣leetcode 31.下一个排列
golang力扣leetcode 31.下一个排列
22 0
|
3天前
|
Go
golang力扣leetcode 567.字符串的排列
golang力扣leetcode 567.字符串的排列
23 0
|
3天前
|
算法
【LeetCode】31. 下一个排列【中等】
【LeetCode】31. 下一个排列【中等】
|
9月前
|
算法
LeetCode 算法 | 如何排列硬币?
LeetCode 算法 | 如何排列硬币?
|
11月前
|
算法 安全 Swift
LeetCode - #60 排列序列
不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。如果大家有建议和意见欢迎在文末留言,我们会尽力满足大家的需求。
|
11月前
|
算法 安全 Swift
LeetCode - #31 下一个排列 (Top 100)
不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。如果大家有建议和意见欢迎在文末留言,我们会尽力满足大家的需求。
|
11月前
|
数据安全/隐私保护
LeetCode 1734. 解码异或后的排列
LeetCode 1734. 解码异或后的排列
50 0