【LeetCode】解题31:Next Permutation

LeetCode - 解题笔记 - 31 - Next Permutation Next Permutation Solution 1 这个是官方的题解,我一直对permutation的大小比较改进不是很理解,所以只能先学习了(permutation的基本操作就是替换和逆序,这个应该大家都懂,指示怎么确定字典序我一直搞不太懂)。 题目要求寻找当前序列字典序的最近的较大方案,因此首先要找到一组升序对,将其替换后会变大。这个部分是比较好想的,实现过程也是从右侧开始找升序对替换之,能够保证替换的逆序对是所有里面最小的(但不是所有permutation里面最小的)。 为了保证最小的大方案,就要 阅读详情

Problem 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 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

来源:LeetCode

解题思路

下一个更大的排列应该从改变数值后影响最小的位数考虑。

  • 第一层循环中,设置指针i从后往前考察,是否有某一位上的数nums[i],其值可以通过与一个更小位上的数 K K K( K ∈ K\in K∈nums[i+1]~nums[n-1])做交换而变大,并且变大的程度最小,这要求:
    K = min ⁡ ( { n u m s [ j ] ∣ n u m s [ j ] > n u m s [ i ] , j ∈ [ i + 1 , n − 1 ] } ) K = \min(\{nums[j]\mid nums[j]>nums[i],j\in[i+1,n-1]\}) K=min({nums[j]∣nums[j]>nums[i],j∈[i+1,n−1]})
  • 第二层循环中,由于之前没有找到可以改变数值的位,说明指针i后的数按降序排列,则当从后往前寻找比nums[i]大的数值时,但凡存在这样的数nums[j],都一定是nums[i+1]~nums[n-1]中符合要求的最小数值。
  • 找到可以交换的数nums[j]后,与nums[i]做交换,此时的nums[i+1]~nums[n-1]仍然保持降序排列,只要使用双指针将其改为升序排列,即是符合题意的下一个更大排列。
  • 此外,当排列已经是降序排列时,同样只需用双指针法改为升序排列即可。

两层循环时间复杂度为O(n2),双指针法排序时间复杂度O(n),因此总时间复杂度O(n2)。

要点:双指针

Solution (Java)

class Solution {
    public void nextPermutation(int[] nums) {
        int N = nums.length;
        int temp, l, r;
        for(int i = N-2; i >= 0; i--){
            for(int j = N-1; j > i; j--){
                if(nums[j] > nums[i]){
                    temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                    //sort nums[i+1]~nums[N-1](descending order) in ascending order
                    l = i+1;
                    r = N-1;
                    while(l < r){
                        temp = nums[l];
                        nums[l] = nums[r];
                        nums[r] = temp;
                        l++;
                        r--;
                    }
                    return;
                }
            }
        }
        l = 0;
        r = N-1;
        while(l < r){
            temp = nums[l];
            nums[l] = nums[r];
            nums[r] = temp;
            l++;
            r--;
        }
    }
}

修改过程

  • 一开始的思路是使用指针i从后往前检查,在i之前是否有比nums[i]小的数值nums[j],小就替换,然后将j之后的数从小到大排序。若输入[4,2,0,2,3,2,0],则这种思路输出[4,2,2,0,0,2,3],正确答案应该是[4,2,0,3,0,2,2]。
STL next_permutation和prev_permutation 一、next_permutation的实现 **原理:**所谓的下一个排列实际就是按照字典顺序比该排列更大的一个排列。首先从后往前找一个递增序列(递增序列说明在该递增序列里这是按照字典排序的最大序列),并找到第一个非递增的索引 i。而为了得到一个字典序列更大的序列,就必须从递增序列中找一个稍微大于索引 i 对应的数值,并与它替换;并反转递增序列变为递减序列为更新后的最小序列,从而得到下一个排列。 ... 阅读详情

相关推荐

发那科机器人编程软件fanuc roboguide授权补丁_工业机器人离线编程与应用:ROBOGUIDE V8.3版本的工程文件创建...

欢迎关注我们ROBOGUIDE是发那科机器人公司提供的一个离线编程工具,可模拟现实中的机器人和周边设备的布局,通过虚拟TP示教来模拟机器人的运动轨迹,同时可以验证方案的可行性和准确的周期时间。ROBOGUIDE是一款核心应用软件,具体的还包括搬运、弧焊、喷涂等其他模块。以最新的8.3版本为例进行讲解,对于未注册的版本将有30天的免费使用期限。开始菜单击开始菜单中的HandingPRO启动程序,第一...

weixin_39791446的博客 2924

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

微信中调起支付宝支付

微信中调用支付宝支付,首先我们知道微信是不能直接调用支付宝的,所以我们需要使用一个中间页提示用户在浏览器中打开,然后进行网页支付或打开app支付。以下为详细唤起支付宝的步骤、业务代码以及相关页面图。 如有问题欢迎评论留言,看到定回复哦^_^\(^o^)/~ 一、微信中唤起支付宝,有几个步骤: 1、中间页引导用户在浏览器中打开去唤起支付宝。 2、浏览器中打开支付宝或app中打开支付宝。 3...

han_calligrapher的博客 2万+

[LeetCode]31. Next Permutation

[LeetCode]31. Next Permutation题目描述思路开始的思路:找到最靠右的 nums[i -1]小于nums[i],并在nums[i]的右边找出小于nums[i]并且大于nums[i - 1]的数,将此数与nums[i]交换,之后递归,这个思路会导致一些比较简单的情况无法出来 后续: 例: 数组 1 4 2 3 2 1 index 0

Lcharon的博客 3062

QT的温湿度显示系统

大家好,这里是石侃侃的博客,欢迎前来讨论这篇文章介绍的是DHT11在2440上的基于Qt5.6的应用基于单片机系统的温湿度监测系统构成框图如图2-1所示。图2-1 系统构成框图该系统采用单片机s3c2440作为了本设计的核心元件。利用LCD作为了显示器件,利用触摸屏作为用户界面输入设备,第一行显示当前实时的温度,湿度,第二行依次显示历史时刻最高的温度值,最高的湿度值。第三行是一个文本框,记录了程...

zxpcus的博客 1万+

SV fork join_none & for循环结合使用

fork join_none与循环联用常见问题解决方案

ZhangShuHuai11的博客 880

LeetCode 31. Next Permutation

题目来源:https://leetcode.com/problems/next-permutation/ 问题描述 31.Next Permutation Medium Implementnext permutation, which rearranges numbers into the lexicographically next greater permutation of nu...

da_kao_la的博客 228

刷爆 Java 基础!1000 道经典面试题精解第一篇【序列化】

自定义序列化 / 反序列化的实现方式在实现接口的类中,手动重写和方法在方法内优先调用执行默认序列化逻辑;追加自定义逻辑(如敏感数据加密、临时字段赋值、兼容旧版本数据等)。// 序列化ID,保证兼容性// 敏感字段,默认不序列化,自定义处理// 自定义序列化// 执行默认序列化:写入username// 自定义逻辑:密码加密后写入// 自定义反序列化// 执行默认反序列化:读取username// 自定义逻辑:读取加密密码并解密// 模拟加密/解密工具方法。

七叔的博客 191

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

Attention优化|2w字原理&图解: 从Online-Softmax到FlashAttention V1/V2/V3

本文通过原理分析和图解的方式,通俗易懂地FlashAttention系列算法。FlashAttention V1/V2在LLM领域的应用已经非常广泛,相关的论文也反复读了几遍。FA1和FA2论文非常经典,都推荐读一下(不过FA2论文中公式错误不少本文大约1.8w字,包括以下内容:0x07 分布式训推使用FlashAttention0x09 FlashAttention中MQA/GQA以及Causal Mask处理。

具身机器人和通用AI 5232

跟踪雷达点迹处理

1,点迹定义: 假设研究对象为位置x和速度v,预测值是根据这一时刻的x值、v值来预测下一时刻的x、v;测量值是根据传感器也就是雷达得到的,因为各种误差的存在,这两个值都不准确,所以把他们加权融合,得到一个新的量—状态值。 点迹就是所谓的测量值,由雷达对同一个目标探测到的一系列点(假设目标静止,但由于误差的存在,会扫描出不止一个点),航迹对应状态值,是点迹和预测轨迹的加权融合。 2,雷达信号和点迹处理关系: 3,点迹航迹处理流程 4,仿真 clear all; RealData=G

wangjie36的博客 1万+

【Leetcode】31. Next Permutation

题目地址: https://leetcode.com/problems/next-permutation/ 给定一个长度为nnn的数组AAA,该数组数字的所有排列中,每个排列都有个字典序。求字典序比AAA大的排列中字典序最小的那个。同一个数组的两个排列PPP和QQQ,P<QP<QP<Q当且仅当存在i≥0i\ge 0i≥0使得P[0]=Q[0],P[1]=Q[1],...,P[i−1]=Q[i−1],P[i]<Q[i]P[0]=Q[0],P[1]=Q[1],...,P[i-1]=Q[i

数学、算法爱好者的博客 419

ROS从入门到精通1-1:ROS核心架构、常用指令与计算图

本文介绍ROS的核心架构,各个文件、目录的用途、常用命令行工具、动态计算图原理。

FRIGIDWINTER的博客 6369

[leetcode javascript解题]Next Permutation

leetcode 第31题 “Next Permutation”描述如下,有点长: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible,

zzxboy1的博客 641

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Leetcode C++ 31. Next Permutation

Leetcode 31. Next Permutation 解题思路:输出字典序中的下一个排列。比如123生成的全排列是:123,132,213,231,312,321。那么321的next permutation是123。下面这种算法据说是STL中的经典算法。在当前序列中,从尾端往前寻找两个相邻升序元素,升序元素对中的前一个标记为k - 1。然后再从尾端寻找另一个大于k - 1的元素,并与k -...

sinat_35177634的博客 272

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Leetcode #31. Next Permutation 下一个全排列 解题报告

1 解题思想这道题是给定一个排序,让你找出使用字典序的方式排序后得到的下一个全排列 什么是字典序排序? 字典排序(lexicographical order)是一种对于随机变量形成序列的排序方法。其方法是,按照字母顺序,或者数字小大顺序,由小到大的形成序列。 其实也就是将全排列按照字典序的方式,得到一个递增的序列。那么我们如何确定下一个字典序?做法,对于当前序列有: 1、找到从右边(末

MebiuW的专栏 1229

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

31. Next Permutation

题目:31. Next Permutation 下一个排列 难度:中等 Implementnext permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must...

qq_21963133的博客 284

leetcode-31. Next Permutation

leetcod-31. Next Permutation题目: 首先,找下一个排序,首先应该找低位的逆排序,因为排序都是从尾部开始变化排序的,也就是对应文中的从右往左找第一个相邻的增长2长度字节first和second。如果找到了就是普通的情况继续下一步,如果没找到则说明整个数组都是逆序的,那这样的情况下一个排序就是将整个数组翻转过来,也就是第三步。 其次,找到当前需要进行转换的位置,那么下一步需要找的就

千念飞羽的专栏 403

LeetCode:31. Next Permutation

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

Wisimer 153

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

【Leetcode】31. Next Permutation 解题报告

【Leetcode】31. Next Permutation 解题报告 题面 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbe...

birduni的博客 238

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

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

水果君の日常 2746

leetcode解题之31. Next Permutation java 版(下一个排列)

leetcode解题之31. Next Permutation java 版(下一个排列)

mine_song的博客 1575

【LeetCode】31. Next Permutation 解题报告(Python & C++)

【LeetCode】31. Next Permutation 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/next-permutation...

负雪明烛 6989

leetcode 31 Next Permutation

找到下一个permutation是什么,对于一个数组[1,2,3],下一个排列就是[1, 3, 2]

xxxmmc的博客 400

leetcode记录31. Next Permutation

31. Next Permutation √ Accepted √ 265/265 cases passed (8 ms) √ Your runtime beats 100 % of cpp submissions √ Your memory usage beats 100 % of cpp submissions (8.7 MB) /* * @lc app=leetcode i...

我的博客 161
上一篇: 【LeetCode】解题27:Remove Element
下一篇: 【LeetCode】解题33:Search in Rotated Sorted Array
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值