当前位置:网站首页>leetcode T31:下一排列
leetcode T31:下一排列
2022-07-01 08:09:00 【范谦之】
题目描述
整数数组的一个 排列 就是将其所有成员以序列或线性顺序排列。
例如,arr = [1,2,3] ,以下这些都可以视作 arr 的排列:[1,2,3]、[1,3,2]、[3,1,2]、[2,3,1] 。
整数数组的 下一个排列 是指其整数的下一个字典序更大的排列。更正式地,如果数组的所有排列根据其字典顺序从小到大排列在一个容器中,那么数组的 下一个排列 就是在这个有序容器中排在它后面的那个排列。如果不存在下一个更大的排列,那么这个数组必须重排为字典序最小的排列(即,其元素按升序排列)。例如,arr = [1,2,3] 的下一个排列是 [1,3,2] 。
类似地,arr = [2,3,1] 的下一个排列是 [3,1,2] 。
而 arr = [3,2,1] 的下一个排列是 [1,2,3] ,因为 [3,2,1] 不存在一个字典序更大的排列。
给你一个整数数组 nums ,找出 nums 的下一个排列。必须 原地 修改,只允许使用额外常数空间。
示例 1:
输入:nums = [1,2,3]
输出:[1,3,2]
示例 2:
输入:nums = [3,2,1]
输出:[1,2,3]
示例 3:
输入:nums = [1,1,5]
输出:[1,5,1]
提示
1 <= nums.length <= 100
0 <= nums[i] <= 100
思路
首先从右往左找,找到一个下标 i n d ind ind,使其右边所有数都是降序排列。例如 :
↓
1432, ind=0
随后,在 [ i n d + 1 , n u m s . l e n g t h − 1 ] [ind+1, nums.length-1] [ind+1,nums.length−1]的书中选取一个最小的且大于 a [ i n d ] a[ind] a[ind] 的数,与 a [ i n d ] a[ind] a[ind] 交换位置,并将 i n d ind ind 右边的数进行升序排列。
代码
int findInd(int[] a) {
// 从右往左找,找到一个下标,其右边所有数字都是从大到小,如1.432,
// 那么,需要从432(即所有逆序排列的数中找到一个最小的,即2),替换1,并且让143升序排列
int ind = a.length-1;
while(ind > 0) {
if(a[ind] <= a[ind-1]) ind--;
else break;
}
return ind-1;
}
void next_permutation(int[] a) {
int ind = findInd(a);
if(ind == -1) {
Arrays.sort(a);
}
else {
int tar = -1;
for(int i = a.length-1; i > ind; i--) {
if(a[i] > a[ind]) {
tar = i;
break;
}
}
int t = a[ind]; a[ind] = a[tar]; a[tar] = t;
Arrays.sort(a, ind+1, a.length);
}
}
边栏推荐
- [website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions
- Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization
- web254
- Aardio - 自己构造的getIconHandle的方法
- web254
- Hijacking a user's browser with beef
- Cyclic neural network
- Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
- window c盘满了
- Li Kou daily question - day 31 -202 Happy number
猜你喜欢

web254

Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization

Access report realizes subtotal function

凸印的印刷原理及工艺介绍

Significance and measures of source code encryption
![[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions](/img/2c/07d729d49b1d74553decac4588074e.png)
[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions

QT -- 1. QT connection database

postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql

Principle and process of embossing

Hijacking a user's browser with beef
随机推荐
【Redis】一气呵成,带你了解Redis安装与连接
Provincial selection + noi Part II string
LM08丨网格系列之网格反转(精)
getInputStream() has already been called for this request
P4 安装bmv2 详细教程
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
[staff] key number (key number identification position | key number marking list | a major key identification principle | F, C, G position marking ascending | F major key identification principle | B
Source code analysis of open source API gateway APIs IX
力扣每日一题-第32天-1822.数组元素积的符号
ContentType所有类型对比
EDA开源仿真工具verilator入门6:调试实例
Book of quantitative trading - reading notes of the man who conquers the market
Microsoft stream - how to modify video subtitles
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)
使用 setoolkit 伪造站点窃取用户信息
Insufficient executors to build thread pool
7-26 word length (input and output in the loop)
Li Kou daily question - day 31 -202 Happy number
【刷题】字符统计【0】
栈实现计算器