当前位置:网站首页>leetcode T31:下一排列
leetcode T31:下一排列
2022-07-01 08:17: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);
}
}
边栏推荐
- shardingSphere
- 程序员养生宝典
- SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?
- Anddroid 文本合成语音TTS实现
- Chinese font Gan: zi2zi
- Learn reptiles for a month and earn 6000 a month? Tell you the truth about the reptile, netizen: I wish I had known it earlier
- PHP laravel wechat payment
- 0 basic introduction to single chip microcomputer: how to use digital multimeter and precautions
- Latex table
- [force deduction 10 days SQL introduction] Day9 control flow
猜你喜欢
![[staff] high and low octave mark (the notes in the high octave mark | mark range are increased by one octave as a whole | low octave mark | mark range are decreased by one octave as a whole)](/img/ff/ebd936eaa6e57b1eabb691b0642957.jpg)
[staff] high and low octave mark (the notes in the high octave mark | mark range are increased by one octave as a whole | low octave mark | mark range are decreased by one octave as a whole)

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

Use threejs simple Web3D effect

Teach you how to apply for domestic trademark online step by step
![[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order](/img/87/07783593dbabcf29700fa207ecda08.png)
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order

Erreur de hauteur du clavier souple

Conception et mise en service du processeur - chapitre 4 tâches pratiques
![[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

网关gateway-88

Microsoft stream - how to modify video subtitles
随机推荐
Php laraver Wechat payment
谈谈数字化转型的几个关键问题
Set up file server Minio for quick use
Serial port oscilloscope software ns-scope
Scala language learning-07-constructor
XX攻击——反射型 XSS 攻击劫持用户浏览器
防“活化”照片蒙混过关,数据宝“活体检测+人脸识别”让刷脸更安全
Utiliser Beef pour détourner le navigateur utilisateur
[redis] it takes you through redis installation and connection at one go
CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging
Learn the knowledge you need to know about the communication protocol I2C bus
手工挖XSS漏洞
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
如何使用layui将数据库中的数据以表格的形式展现出来
Provincial election + noi part I dynamic planning DP
SQL number injection and character injection
OJ input and output exercise
01 NumPy介绍
How outlook puts together messages with the same discussion
Source code analysis of open source API gateway APIs IX