当前位置:网站首页>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);
}
}
边栏推荐
- Set up file server Minio for quick use
- Chinese font Gan: zi2zi
- 【入门】截取字符串
- Teach you how to apply for domestic trademark online step by step
- 使用threejs简单Web3D效果
- getInputStream() has already been called for this request
- 【力扣10天SQL入门】Day10 控制流
- [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
- How to check ad user information?
猜你喜欢

Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure

shardingSphere

OJ input and output exercise

一套十万级TPS的IM综合消息系统的架构实践与思考

网关gateway-88

web254
![[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)](/img/48/de19e8cc007b93a027a906d4d423b2.png)
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)

Manually dig XSS vulnerabilities

CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging

5大组合拳,解决校园6大难题,护航教育信息化建设
随机推荐
Airsim雷达相机融合生成彩色点云
How to get a SharePoint online site created using the office365 group template
Microsoft stream - how to modify video subtitles
Li Kou daily question - day 31 -1502 Judge whether an arithmetic sequence can be formed
Scala语言学习-07-构造器
CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging
凸印的印刷原理及工艺介绍
How to use layui to display the data in the database in the form of tables
Airsim radar camera fusion to generate color point cloud
OJ input and output exercise
Provincial election + noi Part VI skills and ideas
Utiliser Beef pour détourner le navigateur utilisateur
Cmake I two ways to compile source files
使用 setoolkit 伪造站点窃取用户信息
Li Kou daily question - day 31 -202 Happy number
seaborn clustermap矩阵添加颜色块
Soft keyboard height error
[introduction] approximate value
【刷题】字符统计【0】
[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions