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

来源:LeetCode

解题思路

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

  • 第一层循环中,设置指针i从后往前考察,是否有某一位上的数nums[i],其值可以通过与一个更小位上的数 K K K K ∈ K\in Knums[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,n1]})
  • 第二层循环中,由于之前没有找到可以改变数值的位,说明指针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]。
OpenResty无损升级内嵌nginx版本0DAY漏洞 OpenResty无损升级内嵌nginx版本,nginx 阅读详情

相关推荐

数据分享|1961-2017年中国0.25°×0.25° 逐日地表水文数据集(VIC-CN05.1)

其中,全球径流中心(University of New Hampshire and Global Runoff Data Centre, UNH/GRDC)的复合产流场集成了观测的河道径流和水量平衡模式模拟的地表和地下径流,常被用来评估模拟的径流。在长白山、海北灌丛和千烟洲通量站,模拟与观测的逐月潜热通量的平均NSE为0.61。VIC-CN05.1受益于较准确的大气驱动场、先进的地表水文参数化方案以及更真实丰富的土壤信息,可作为中国水文气象研究的参照数据,用于陆面模式的评估和长期的趋势分析等。

J_Giser的博客 1781

leetcode题解:第31Next 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的博客 326

YOLOv11改进 - C3k2融合 | C3k2融合细节增强卷积DEConv(Detail-Enhanced Convolution)提升细微特征提取能力

本文介绍了细节增强卷积(DEConv)技术在YOLOv11中的结合应用。DEConv由标准卷积层和多种差异卷积层并行组成,通过计算像素差异和卷积操作增强特征提取能力,还采用重参数化技术简化模型结构。其优势在于能更好捕捉高频信息、保持参数数量不变且降低推理计算成本。我们将DEConv集成进YOLOv11,替换部分模块。

魔改工程师的博客 819

[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的博客 3056

LeetCode 31Next 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

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的博客 209

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

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

[leetcode javascript解题]Next Permutation

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

zzxboy1的博客 635

Leetcode C++ 31. Next Permutation

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

sinat_35177634的博客 267

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的博客 281

LeetCode解题126:Word Ladder II(BFS算法

LeetCode解题 126:Word Ladder II(BFS算法)Problem 126: Word Ladder II [Hard]解题思路Solution (Java)修改过程 Problem 126: Word Ladder II [Hard] Given two words (beginWord and endWord), and a dictionary’s word list, ...

Fayedily的博客 771

LeetCode解题188:Best Time to Buy and Sell Stock IV(动态规划)

LeetCode解题 188:Best Time to Buy and Sell Stock IV (动态规划)Problem 188: Best Time to Buy and Sell Stock IV [Hard]解题思路Solution (Java)修改过程 Problem 188: Best Time to Buy and Sell Stock IV [Hard] Say you hav...

Fayedily的博客 594

LeetCode解题123:Best Time to Buy and Sell Stock III(动态规划)

LeetCode解题 123:Best Time to Buy and Sell Stock III (动态规划)Problem 123: Best Time to Buy and Sell Stock III [Hard]解题思路I. O(2n)时间+O(3n)空间II. O(2n)时间+O(n)空间III. O(n)时间+O(4)空间Solution (Java) Problem 123: B...

Fayedily的博客 487

LeetCode解题119:Pascal's Triangle II

LeetCode解题 118:Pascal's Triangle II Problem 118: Pascal's Triangle II [Easy]解题思路Solution (Java) Problem 118: Pascal’s Triangle II [Easy] Given a non-negative index k where k ≤ 33, return the kth index...

Fayedily的博客 444

LeetCode解题78:Subsets

LeetCode解题 78:Subsets Problem 78: Subsets [Medium]解题思路Solution (Java)修改过程 Problem 78: Subsets [Medium] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The so...

Fayedily的博客 443

LeetCode解题44:Wildcard Matching(动态规划解法)

LeetCode解题 44:Wildcard Matching(动态规划解法)Problem 44: Wildcard Matching [Hard]解题思路Solution (Java) Problem 44: Wildcard Matching [Hard] Given an input string (s) and a pattern (p), implement wildcard patt...

Fayedily的博客 429

LeetCode解题10:Regular Expression Matching(动态规划解法)

LeetCode解题 10:Regular Expression Matching(动态规划解法)Problem 10: Regular Expression Matching [Hard]解题思路Solution (Java) Problem 10: Regular Expression Matching [Hard] Given an input string (s) and a patter...

Fayedily的博客 411

LeetCode解题122:Best Time to Buy and Sell Stock II

LeetCode解题 122:Best Time to Buy and Sell Stock II Problem 122: Best Time to Buy and Sell Stock II [Easy]解题思路Solution (Java) Problem 122: Best Time to Buy and Sell Stock II [Easy] Say you have an array...

Fayedily的博客 365

LeetCode解题32:Longest Valid Parentheses(多解法:栈+动态规划+计数器)

LeetCode解题 32:Longest Valid Parentheses(多解法:栈+动态规划+计数器)Problem 32: Longest Valid Parentheses [Hard]解题思路1. 栈2. 动态规划3. 计数器Solution (Java) Problem 32: Longest Valid Parentheses [Hard] Given a string cont...

Fayedily的博客 352

LeetCode解题714:Best Time to Buy and Sell Stock with Transaction Fee(动态规划)

LeetCode解题 714:Best Time to Buy and Sell Stock with Transaction Fee (动态规划)Problem 714: Best Time to Buy and Sell Stock with Transaction Fee [Medium]解题思路Solution (Java) Problem 714: Best Time to Buy an...

Fayedily的博客 339

500mA电源管理无线充电器ALTIUM设计硬件原理图+PCB文件.zip

500mA电源管理无线充电器ALTIUM设计硬件原理图+PCB文件,2层板设计,大小为45x64mm,双面布局布线,Altium Designer 设计的工程文件,包括完整的原理图及PCB文件,可以用Altium(AD)软件打开或修改,可作为你产品设计的参考。主要型号列表:0402YC132KAT2A CAP, CERM, 1300 pF, 16 V, +/- 10%, X7R, 0402BQ27421YZFR-G1A System-Side Impedance Track Fuel Gauge With Integrated Sense Resistor, YZF0009AKALBQ29700DSER Cost-Effective Voltage and Current Protection Integrated Circuit for Single-Cell Li-Ion/Li-Polymer Batteries, DSE0006ABQ51050BRHL High-Efficiency Qi v1.1-Compliant Wireless Power Receiver and Battery Charger, RHL0020AC0402C103J5RACTU CAP, CERM, 0.01 礔, 50 V, +/- 5%, X7R, 0402C0603C106M9PACTU CAP, CERM, 10 礔, 6.3 V, +/- 20%, X5R, 0603C1005X5R1A475K050BC CAP, CERM, 4.7 礔, 10 V, +/- 10%, X5R, 0402C1005X7R1C223K CAP, CERM, 0.022 礔, 16 V, +/- 10%, X7R, 0402C1005X7R1H683K CAP, CERM, 0.068 礔, 50 V, +/- 10%, X7R, 0402CC0402JRNPO9BN101 CAP, CERM, 100 pF, 50 V, +/- 5%, C0G/NP0, 0402CRCW04020000Z0ED RES, 0, 5%, 0.063 W, 0402CRCW040210K0FKED RES, 10.0 k, 1%, 0.063 W, 0402CRCW040212K4FKED RES, 12.4 k, 1%, 0.063 W, 0402CRCW04021K00FKED RES, 1.00 k, 1%, 0.063 W, 0402CRCW04021K50FKED RES, 1.50 k, 1%, 0.063 W, 0402CRCW04021M00FKED RES, 1.00 M, 1%, 0.063 W, 0402CRCW0402200RFKED RES, 200, 1%, 0.063 W, 0402CRCW040220K0FKED RES, 20.0 k, 1%, 0.063 W, 0402CRCW0402221RFKED RES, 221, 1%, 0.063 W, 0402CRCW0402243KFKED RES, 243 k, 1%, 0.063 W, 0402CRCW04022K21FKED RES, 2.21 k, 1%, 0.063 W, 0402CRCW04022K43FKED RES, 2.43 k, 1%, 0.063 W, 0402CRCW0402422RFKED RES, 422, 1%, 0.063 W, 0402CRCW0402768KFKED RES, 768 k, 1%, 0.063 W, 0402CSD85301Q2 MOSFET, N-CH, 20 V, 6.7 A,ERJ-L03KF47MV RES, 0.047, 1%, 0.1 W, 0603GRM155R60J474KE19D CAP, CERM, 0.47 礔, 6.3 V, +/- 10%, X5R, 0402GRM155R61A105KE15D CAP, CERM, 1 礔, 10 V, +/- 10%, X5R, 0402GRM155R71A104KA01D CAP, CERM, 0.1 礔, 10 V, +/- 10%, X7R, 0402GRM155R71H223KA12D CAP, CERM, 0.022 礔, 50 V, +/- 10%, X7R, 0402GRM1885C1H182

物联网:开源物联网平台-设备管理,数据收集,处理和可视化

东西板 ThingsBoard是用于数据收集,处理,可视化和设备管理的开源物联网平台。文献资料ThingsBoard文档托管在。物联网用例 入门遵循本在几分钟之内收集并可视化您的IoT数据。支持执照该项目根据。

上一篇: 【LeetCode】解题27:Remove Element
下一篇: 【LeetCode】解题33:Search in Rotated Sorted Array
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值