当前位置:网站首页>Leetcode t31: next spread
Leetcode t31: next spread
2022-07-01 08:17:00 【Fan Qianzhi】
Title Description
An integer array array Is to arrange all its members in sequence or linear order .
for example ,arr = [1,2,3] , The following can be regarded as arr Permutation :[1,2,3]、[1,3,2]、[3,1,2]、[2,3,1] .
Integer array Next spread It refers to the next lexicographic order of its integers . More formally , Arrange all the containers in the order from small to large , So the array of Next spread It is the arrangement behind it in this ordered container . If there is no next larger arrangement , Then the array must be rearranged to the lowest order in the dictionary ( namely , Its elements are arranged in ascending order ).for example ,arr = [1,2,3] The next line up for is [1,3,2] .
Similarly ,arr = [2,3,1] The next line up for is [3,1,2] .
and arr = [3,2,1] The next line up for is [1,2,3] , because [3,2,1] There is no greater order of dictionaries .
Give you an array of integers nums , find nums The next permutation of . must In situ modify , Only additional constant spaces are allowed .
Example 1:
Input :nums = [1,2,3]
Output :[1,3,2]
Example 2:
Input :nums = [3,2,1]
Output :[1,2,3]
Example 3:
Input :nums = [1,1,5]
Output :[1,5,1]
Tips
1 <= nums.length <= 100
0 <= nums[i] <= 100
Ideas
First, look from right to left , Find a subscript i n d ind ind, Make all the numbers on the right of it in descending order . for example :
↓
1432, ind=0
And then , stay [ 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] Choose a book that is the smallest and greater than a [ i n d ] a[ind] a[ind] Number of numbers , And a [ i n d ] a[ind] a[ind] Swap places , And will i n d ind ind The numbers on the right are arranged in ascending order .
Code
int findInd(int[] a) {
// Look from right to left , Find a subscript , All the numbers on the right are from big to small , Such as 1.432,
// that , Need from 432( That is, find the smallest of all the numbers in reverse order , namely 2), Replace 1, And let 143 Ascending order
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);
}
}
边栏推荐
- leetcode T31:下一排列
- Anddroid 文本合成语音TTS实现
- How to prevent the other party from saying that he has no money after winning the lawsuit?
- How outlook puts together messages with the same discussion
- How to get a SharePoint online site created using the office365 group template
- 防“活化”照片蒙混过关,数据宝“活体检测+人脸识别”让刷脸更安全
- Airsim雷达相机融合生成彩色点云
- getInputStream() has already been called for this request
- 软键盘高度报错
- Latex formula code
猜你喜欢

Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization

Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation
![[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

Learn the knowledge you need to know about the communication protocol I2C bus

How outlook puts together messages with the same discussion

Erreur de hauteur du clavier souple

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

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

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

【刷题】字符统计【0】
随机推荐
Codeworks round 803 (Div. 2) VP supplement
golang中的正则表达式使用注意事项与技巧
Why are some Wills made by husband and wife invalid
Analysis of slice capacity expansion mechanism
EDA开源仿真工具verilator入门6:调试实例
php laravel微信支付
Gdip - hatchbrush pattern table
Provincial election + noi Part III tree problems
Leetcode T40: 组合总和II
Aardio - [problem] the problem of memory growth during the callback of bass Library
How to use layui to display the data in the database in the form of tables
CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging
[getting started] input n integers and output the smallest K of them
Access report realizes subtotal function
【入门】提取不重复的整数
Learn the knowledge you need to know about the communication protocol I2C bus
Scala language learning-07-constructor
How can beginners correctly understand Google's official suggested architectural principles (questions?)
Transaction method call @transactional
5大组合拳,解决校园6大难题,护航教育信息化建设