当前位置:网站首页>Leetcode t31: prochain arrangement
Leetcode t31: prochain arrangement
2022-07-01 08:17:00 【Fan Qianzhi】
Description du sujet
Un tableau entier Disposition Est d'organiser tous ses membres dans un ordre séquentiel ou linéaire.
Par exemple,arr = [1,2,3] ,Ce qui suit peut être considéré comme arr Disposition:[1,2,3]、[1,3,2]、[3,1,2]、[2,3,1] .
Du tableau entier Ordre suivant Est l'ordre plus grand du dictionnaire suivant de ses entiers.Plus officiellement,Si tous les arrangements d'un tableau sont disposés dans un conteneur dans l'ordre de son dictionnaire, du plus petit au plus grand,Donc le tableau Ordre suivant C'est l'arrangement derrière ce conteneur ordonné.S'il n'y a pas d'arrangement plus grand,Alors ce tableau doit être réarrangé dans le plus petit ordre de Dictionnaire(C'est - à - dire:,Ses éléments sont classés par ordre croissant).Par exemple,arr = [1,2,3] L'ordre suivant est [1,3,2] .
De même,,arr = [2,3,1] L'ordre suivant est [3,1,2] .
Et arr = [3,2,1] L'ordre suivant est [1,2,3] ,Parce que [3,2,1] Il n'y a pas d'ordre de dictionnaire plus grand.
Pour vous donner un tableau entier nums ,Trouver nums L'arrangement suivant de.Il faut Restez où vous êtes. Modifier,Seuls les espaces constants supplémentaires sont autorisés.
Exemple 1:
Entrée:nums = [1,2,3]
Produits:[1,3,2]
Exemple 2:
Entrée:nums = [3,2,1]
Produits:[1,2,3]
Exemple 3:
Entrée:nums = [1,1,5]
Produits:[1,5,1]
Conseils
1 <= nums.length <= 100
0 <= nums[i] <= 100
Idées
D'abord de droite à gauche. ,Trouver un indice i n d ind ind, Que tous les nombres sur sa droite soient dans l'ordre décroissant .Par exemple :
↓
1432, ind=0
Par la suite,In [ 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] Choisissez l'un des plus petits et plus grands livres de a [ i n d ] a[ind] a[ind] Nombre de,Avec a [ i n d ] a[ind] a[ind] Changer de position,Et va i n d ind ind Les nombres à droite sont dans l'ordre croissant .
Code
int findInd(int[] a) {
// Cherchez de droite à gauche,Trouver un indice, Tous les chiffres à droite vont de grands à petits ,Par exemple:1.432,
// Alors,Besoin de432( C'est - à - dire qu'un des plus petits de tous les nombres dans l'ordre inverse ,C'est - à - dire:2),Remplacer1,Et laisser143Ordre croissant
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);
}
}
边栏推荐
- Count number of rows per group and add result to original data frame
- Aardio - Method of self constructed geticonhandle
- [untitled]
- Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
- Comprehensive experiment Li
- Differential: definition of total differential, partial derivative, gradient
- OJ输入输出练习
- CPU设计实战-第四章实践任务一简单CPU参考设计调试
- ContentType所有类型对比
- Yolov5进阶之六目标追踪环境搭建
猜你喜欢

P4 installation bmv2 detailed tutorial

Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation

Access report realizes subtotal function
![[dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)](/img/3e/75a1152f9cdf63c6779fdadec702a0.jpg)
[dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)

01 NumPy介绍

Manually dig XSS vulnerabilities

Koltin35, headline Android interview algorithm
![[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion](/img/ce/6c9e4f2c54710610e8b1f68d6d8088.png)
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion

01 numpy introduction

凸印的印刷原理及工艺介绍
随机推荐
Sqlalchemy creating MySQL_ Table
Adding color blocks to Seaborn clustermap matrix
Koltin35, headline Android interview algorithm
Rumtime 1200 upgrade: London upgrade support, pledge function update and more
Codeforces Round #803 (Div. 2) VP补题
[force deduction 10 days SQL introduction] Day9 control flow
使用beef劫持用戶瀏覽器
seaborn clustermap矩阵添加颜色块
When using charts to display data, the time field in the database is repeated. How to display the value at this time?
Anddroid 文本合成语音TTS实现
Implementation and encapsulation of go universal dynamic retry mechanism
5大组合拳,解决校园6大难题,护航教育信息化建设
量化交易之读书篇 - 《征服市场的人》读书笔记
Airsim radar camera fusion to generate color point cloud
01 numpy introduction
Significance and measures of source code encryption
Provincial election + noi Part III tree problems
Stack implementation calculator
数字转excel的字符串坐标
Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization