LeetCode:31. Next Permutation

word 2013 长篇文档排版案例教程 office 2013 制作长文格式模板 阅读详情

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 replacement must be in-place and use only constant extra memory.

题目的意思就是给出一串数字,求出它的全排列方式中与它本身最接近的一个比它大的数字排列。如果已经是最大的排列方式了,则返回全排列中最小的那种排列方式。例如:

1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
思路一:暴力求解

先求出全排列的所有情况,然后找出比nums大的下一个排列。但是这种情况时间超出,不能ac,仅供参考。

Python 代码实现

class Solution:
    
    def getSortedPermutation(self, nums: List[int]) -> List:
        
        ansSet = {}
        if len(nums) == 1:
            return [nums]
        
        lastArray = self.getSortedPermutation(nums[0:len(nums)-1])
        for last in lastArray:
            l = len(last)
            
            for i in range(l+1):
                tmpLast = last[:]
                tmpLast.insert(i,nums[-1])
                lastKey = self.toString(tmpLast)
                ansSet[lastKey]=tmpLast
        
        ans = [List for i in range(len(ansSet))]
        index = 0
        for s,value in ansSet.items():
            ans[index] = value
            index+=1
        
        return ans
        
    def toString(self,nums: List[int]) -> str:
        return "".join(str(x) for x in nums)
        
    def nextPermutation(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        allAns = self.getSortedPermutation(nums)
        allAns.sort()
        res = 0
        for i in range(len(allAns)):
            if self.toString(allAns[i]) == self.toString(nums):
                res = i
        if res == len(allAns) - 1:
            res = 0
        else:
            res+=1
        for i in range(len(nums)):
            nums[i] = allAns[res][i]
思路二

由于要找的是比当前数大得最接近的那个数,所以假设后面的某一段是降序,只要在这个降序部分找到一个数字大于这段的前面那个数字,然后交换一下这两个数,在将降序部分翻转,即可得到最终结果。

下面这个动图很直观:

1

就是从后向前查找,知道找出相邻两个是降序的数字,比如这里的(7,4),然后取较小的那个。然后再在它的后面的比它(4)大的数中找到最小的那个(也就是刚好比4大的一个数),这里就是5,然后交换这两个树的位置,也就是4和5交换,最后对5后面那部分进行升序排列。这样就得到了结果。

Python 代码实现

import copy
class Solution:
    def swap(self,nums: List[int],  i,  j):
        temp = nums[i]
        nums[i] = nums[j]
        nums[j] = temp
        
    def reverse(self,nums: List[int],  start):
        i = start
        j = len(nums) - 1
        while (i < j) :
            self.swap(nums, i, j)
            i+=1;
            j-=1;
        
    def nextPermutation(self, nums: List[int]) -> None:
        i = len(nums) - 2
        # 先找到降序的那个数x
        while (i >= 0 and nums[i + 1] <= nums[i]):
            i-=1
        
        # 由于上面的代码已经保证了x后面都是降序。
        # 所以只要从最后一位向前遍历,找到第一个比x小的数即可。
        if (i >= 0) :
            j = len(nums) - 1
            while (j >= 0 and nums[j] <= nums[i]):
                j-=1
            
            self.swap(nums, i, j)
        # 升序排列后部分
        self.reverse(nums, i + 1)

THE END.

C++和OpenGL实现3D游戏编程【连载6】——不规则图形的纹理贴图(附源码) 上一节我们讨论了纹理贴图的相关基础操作,但上一节的纹理贴图操作基本上都是规则图形,包括圆形和球形虽然复杂一点,但是它也是规则的。这一节课我们要讨论一下,怎么在不规则图形上纹理贴图,就比如文章下图的心形纹理贴图,以及纹理的平移、旋转、放大缩小功能。 阅读详情

相关推荐

手把手教学|用飞书+扣子工作流实现自动化批量图片生成,效率提升99%!体验完整操作流程

在先前的文章中,我们已经深入探讨了扣子智能体的多种应用场景,以及如何创建和使用工作流进行高效管理。本文将引领大家通过一个具体案例,将扣子的工作流功能嵌入飞书应用中,实现操作的自动化与高效化。

weixin_66485800的博客 6028

【LeetCode】解题31:Next Permutation

LeetCode解题 31:Next Permutation Problem 31: Next Permutation [Medium]解题思路Solution (Java)修改过程 Problem 31: Next Permutation [Medium] Implement next permutation, which rearranges numbers into the lexicogr...

Fayedily的博客 357

B-Tree数据存储

SQLITE数据存储1. B-Tree的优势  在SQLite中数据库、表、索引都是通过B-Tree和B+Tree的形式进行存储的。如果采用传统的二叉树查找树、红黑树的查找深度一般为log2(N) \log{_2}(N)(其中N表示的是整个树的关键字的数目),在数据库中如果存储的资源过多,就会导致查找一个数据过程中出现较多的磁盘I/O操作,而过多的磁盘操作就会导致整个查找的效率较低,而基于二叉树的一

Promiscuous_youth的博客 1141

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 t...

chenf1999的博客 330

开源收银系统_大型收银系统源码_OctShop

开源收银系统正成为大型连锁企业数字化转型的关键工具。相比传统闭源系统,开源方案具有成本低(年均降本60%+)、定制灵活(支持多业态协同)、自主可控等优势,能有效解决高并发、多门店数据同步等痛点。核心价值在于:1)分布式架构适配规模化扩张;2)模块化设计实现个性化开发;3)开放接口打通ERP/CRM等系统。典型案例显示,连锁商超通过源码二次开发实现库存实时同步,补货效率提升50%;餐饮集团统一多业态收银流程后会员复购率增长35%。选型需关注源码成熟度、技术兼容性和安全合规,建议采用"开源+服务商&q

OctShop全栈工程师 938

STM32F103C8T6驱动DM542步进电机:从CubeMX配置到按键控制的全流程避坑指南

本文详细介绍了如何使用STM32F103C8T6通过CubeMX配置驱动DM542步进电机的全流程,包括PWM频率计算、GPIO模式选择及按键控制实现。重点解决了常见硬件连接错误、PWM参数配置及加减速算法优化,帮助开发者快速搭建稳定的电机控制系统。

weixin_42741930的博客 308

Leetcode1-100: 31. Next Permutation

Leetcode1-100: 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

Y123iwantit的博客 212

深入解析时钟同步技术:相位同步、频率同步与同源时钟的实战应用

本文深入解析时钟同步技术的三种核心方式:相位同步、频率同步与同源时钟,详细阐述其技术原理、实现方案及实战应用。通过对比分析不同同步方式的特点与适用场景,为电子系统设计提供选型指南,帮助工程师优化时钟网络设计,提升系统稳定性和性能。

weixin_29240711的博客 449

LeetCode 31: Next Permutation

题目描述: mplement 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...

dez62024的博客 171

Unreal Engine开发:角色动画与状态机_动画混合:通过混合节点实现平滑动画过渡

动画混合是提升游戏动画质量的关键技术之一。Unreal Engine提供了多种混合节点,包括BlendSpace1D、Blend Two By Bool、Blend Two By Alpha和Sequencer,每种节点适用于不同的场景。通过合理使用这些节点,可以实现平滑、自然且高效的动画过渡效果。此外,遵循最佳实践和高级应用技巧,可以进一步优化动画质量和性能。希望本节内容对你在Unreal Engine中实现动画混合有所帮助,使你的游戏角色动作更加流畅和自然。

chenlz2007的博客 1245

Response Instance 详解:在 ABAP OData Client Proxy 中读懂每一次响应

ABAP OData Client Proxy中的Response Instance是关键信息载体,封装了业务数据及调用上下文(如分页、delta link等)。响应实例在执行请求后自动生成,根据请求类型返回特定接口(如READ返回/IWBEP/IF_CP_RESPONSE_READ)。响应抽象与OData版本解耦,支持统一处理V2/V4服务。常见响应类型包括单实体读取、列表读取(支持分页和增量同步)、创建/更新/删除响应等。开发时可通过响应对象的安全类型方法获取数据,如get_business_data获

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 137

Leetcode: 31. Next Permutation

Description 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 l...

anrui0096的博客 158

【RT-DETR有效改进】利用EMAttention加深网络深度提高模型特征提取能力(特征选择模块)

本文给大家带来的改进机制是EMAttention注意力机制,它的核心思想是,重塑部分通道到批次维度,并将通道维度分组为多个子特征,以保留每个通道的信息并减少计算开销。EMA模块通过编码全局信息来重新校准每个并行分支中的通道权重,并通过跨维度交互来捕获像素级别的关系。本文改进是基于ResNet18、ResNet34、ResNet50、ResNet101,文章中均以提供,本专栏的改进内容全网独一份深度改进RT-DETR非那种无效Neck部分改进,

Snu77的博客 3170

python next permutation_[Leetcode][Python]31: Next Permutation

# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'31: Next Permutationhttps://oj.leetcode.com/problems/next-permutation/Implement next permutation, which rearranges numbers into the lexicogr...

weixin_30073141的博客 157

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

LeetCode(31): Next Permutation (C++)

题目   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 po

zhangpiu的专栏 761

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

leetcode 31:Next Permutation

这道题让我们求下一个排列顺序,有题目中给的例子可以看出来,如果给定数组是降序,则说明是全排列的最后一种情况,则下一个排列就是最初始情况,可以参见之前的博客Permutations 全排列。我们再来看下面一个例子,有如下的一个数组 1  2  7  4  3  1 下一个排列为: 1  3  1  2  4  7 那么是如何得到的呢,我们通过观察原数组可以发现,如果从末尾往前看,数字逐渐变...

李威的博客 228

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

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

onlyou2030的博客 426

LeetCode 31: Next Permutation

字典序来是数学中的排列组合,比如“1,2,3”的全排列,依次是: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 所以题目的意思是,从上面的某一行重排到期下一行,如果已经是最后一行了,则重排成第一行。 但是也不能根据给出的数组中的数字列出所有排列,因为要求不能占用额外的空间。 思路 假设 A = [6 5 4 8 7 5 1], next permutation怎么求 (1...

zero 94

Leetcode-31: 31. Next Permutation

以(4,7,6,2)为例,next_permuation()应该返回(6,2,4,7)。我们先从后往前扫描,直到遇到第一个a[i]&lt;a[i+1]的情形,这里是4,7,说明此处要改,4要移到后面来。那4的位置给谁呢?应该是后面的数里面比4大的数中最小的那个,也就是6。那怎么找到这个6呢?从后往前数(因为这里(7,6,2)肯定是单调递减),直到找到第一个比4大的数。然后把它跟4交换,这里就变成了...

roufoo的博客 295

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 or...

qq_34314736的博客 141

LeetCode: 31. Next Permutation (Medium)

1. 原题链接 https://leetcode.com/problems/next-permutation/description/ 2. 题目要求 给出一个整型数组,让我们给出下一个排序情况。注意以下规则: (1)下一个排列必须比原排列要大。例如“1,2,4,5,3”,下一个排列为“1,3,4,5,2”,比之前的排列要大; (2)如果给出的数组已经按降序排列,则下一个排...

aysk1112的博客 214

Leetcode习题解答:31. Next Permutation

难度:medium 原链接 描述 Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange i

低熵随想 233

[Leetcode][Python]31: Next Permutation

# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'31: Next Permutationhttps://oj.leetcode.com/problems/next-permutation/Implement next permutation, which rearranges numbers into the lex...

aaron7909的博客 208

LeetCode31:Next Permutation

Implementnext 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 o...

卷卷萌的博客 361

leetcode question 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 ...

csdn609387481的博客 204
上一篇: LeetCode:46. Permutations
下一篇: LeetCode:43. Multiply Strings
XP-Code
博客等级 码龄12年 113粉丝 194原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值