当前位置:网站首页>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);
}
}
边栏推荐
- Analysis of slice capacity expansion mechanism
- Access report realizes subtotal function
- 源代码加密的意义和措施
- Aardio - [problem] the problem of memory growth during the callback of bass Library
- Leetcode T40: 组合总和II
- Cmake I two ways to compile source files
- Scala语言学习-07-构造器
- [force deduction 10 days SQL introduction] Day10 control flow
- empirical study and case study
- P4 安装bmv2 详细教程
猜你喜欢

P4 安装bmv2 详细教程

Aardio - Shadow Gradient Text
![[untitled]](/img/b9/6922875009c2d29224a26ed2a22b01.jpg)
[untitled]

Conception et mise en service du processeur - chapitre 4 tâches pratiques

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

On several key issues of digital transformation

图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证

5大组合拳,解决校园6大难题,护航教育信息化建设

Significance and measures of source code encryption

Serial port oscilloscope software ns-scope
随机推荐
Adding color blocks to Seaborn clustermap matrix
[force deduction 10 days SQL introduction] Day10 control flow
Koltin35, headline Android interview algorithm
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
【入门】取近似值
Aardio - 阴影渐变文字
Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
The difference between interceptors and filters
【入门】截取字符串
[untitled]
量化交易之读书篇 - 《征服市场的人》读书笔记
使用beef劫持用户浏览器
SharePoint - modify web application authentication using PowerShell
使用threejs简单Web3D效果
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
初学者如何正确理解google官方建议架构原则(疑问?)
【入门】输入整型数组和排序标识,对其元素按照升序或降序进行排序
On June 30, 2022, the record of provincial competition + national competition of Bluebridge
Aardio - Method of self constructed geticonhandle
Android screen adaptation (using constraintlayout), kotlin array sorting