当前位置:网站首页>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);
}
}
边栏推荐
- Aardio - Shadow Gradient Text
- 【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
- Cyclic neural network
- seaborn clustermap矩阵添加颜色块
- 使用 setoolkit 伪造站点窃取用户信息
- [question brushing] character statistics [0]
- 如何使用layui将数据库中的数据以表格的形式展现出来
- On several key issues of digital transformation
- Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization
- String coordinates of number to excel
猜你喜欢
How to use layui to display the data in the database in the form of tables
凸印的印刷原理及工艺介绍
[getting started] intercepting strings
Gru of RNN
[getting started] extract non repeating integers
P4 installation bmv2 detailed tutorial
Serial port oscilloscope software ns-scope
7-26 word length (input and output in the loop)
Differential: definition of total differential, partial derivative, gradient
【网站架构】一招搞定90%的分布式事务,实打实介绍数据库事务、分布式事务的工作原理应用场景
随机推荐
Insufficient executors to build thread pool
使用threejs简单Web3D效果
[untitled]
empirical study and case study
Why are some Wills made by husband and wife invalid
slice扩容机制分析
Transaction method call @transactional
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
The difference between interceptors and filters
Access报表实现小计功能
Chinese font Gan: zi2zi
Source code analysis of open source API gateway APIs IX
postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
Using settoolkit to forge sites to steal user information
Cmake I two ways to compile source files
Adding color blocks to Seaborn clustermap matrix
Principle and process of embossing
SharePoint - modify web application authentication using PowerShell
Office365 - how to use stream app to watch offline files at any time
Precautions and skills in using regular expressions in golang