当前位置:网站首页>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);
}
}
边栏推荐
- Li Kou daily question - Day 32 -1822 Symbol of array element product
- Rk3399 platform development series explanation (network debugging) 7.30. What will affect the sending process of TCP packets?
- Keithley 2100 software 𞓜 Keithley2400 test software ns SourceMeter
- [untitled]
- 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
- Anddroid text to speech TTS implementation
- How to get a SharePoint online site created using the office365 group template
- Microsoft stream - how to modify video subtitles
- How outlook puts together messages with the same discussion
- Why are some Wills made by husband and wife invalid
猜你喜欢

【Redis】一气呵成,带你了解Redis安装与连接

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

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

Serial port oscilloscope software ns-scope

谈谈数字化转型的几个关键问题

使用beef劫持用户浏览器

Adding color blocks to Seaborn clustermap matrix

Use threejs simple Web3D effect

Gru of RNN

5大组合拳,解决校园6大难题,护航教育信息化建设
随机推荐
SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?
PHP laravel wechat payment
Anddroid text to speech TTS implementation
初学者如何正确理解google官方建议架构原则(疑问?)
Access报表实现小计功能
How can beginners correctly understand Google's official suggested architectural principles (questions?)
【入门】输入n个整数,输出其中最小的k个
Provincial election + noi Part VII computational geometry
OJ input and output exercise
php laravel微信支付
php laravel微信支付
Programmer's regimen
On June 30, 2022, the record of provincial competition + national competition of Bluebridge
事务方法调用@Transactional
0 basic introduction to single chip microcomputer: how to use digital multimeter and precautions
【入门】截取字符串
一套十万级TPS的IM综合消息系统的架构实践与思考
力扣每日一题-第31天-1790.仅执行一次字符串交换能否使两个字符串相等
Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
Using settoolkit to forge sites to steal user information