当前位置:网站首页>LeetCode 31. Next spread
LeetCode 31. Next spread
2022-07-05 09:26:00 【Sasakihaise_】
【 analysis 】 If you want to get an element larger than the current number , We need to exchange the next larger number with the previous smaller number , And the bits exchanged in front should be as low as possible , The numbers exchanged later should be as small as possible , But it has to be bigger than the front . Then it needs to be done in two steps :
1. Find the position of the previous element to be exchanged .
2. Look for the smallest element larger than him from the back .
about 1, Let's look at a situation 5,4,3,2,1, We find that this number is already the largest arrangement , That is, you can't find the position you can exchange in front . But for permutations 3,5,4,2,1, If you cover up 3, hinder 5,4,2,1 It is already the maximum , however 3 This position can be changed , So it is not difficult to find , When a sequence is arranged in reverse order , His composition is already the maximum . If such two elements are found (i, j),nums[i] < nums[j], Explain that you can change nums[i] To make the element bigger . In this way, we find the minimum bit of the element to be exchanged .
about 2, Suppose the insertion position is i, that [i + 1, n) It must be in descending order , Continue traversing from back to front , Just look for the first element larger than the insertion position , This element is greater than i The smallest element of the element at .
After exchange , because [i + 1, n) Or in descending order , He is still a big number , But because of the exchange i Cause of , No matter how they are arranged in the back , Are bigger than the previous one , In order to make the result smaller , Also change this part into ascending order .( You can directly reverse , It's fine too sort)
class Solution {
// Monotonic stack 10:12 10:30
// Looking back and forth , If it's in ascending order , The description cannot be changed later, for example 4 6 5 3 2 1, We cannot change 65321 From your location
// Find the next one
// however 4 6 It's in reverse order , Explain that you can find a comparison in the back 4 Large element exchange to make the exchange larger than before
// The first ratio found from the back 4 The big one is 5, In exchange for 4 and 5 And then get 5 6 4 3 2 1, But now it's not the smallest element
// It can only be said that 5 The element at the beginning must be larger than that just now 4 At the beginning , So we have to deal with 5 The following elements are arranged in order from small to large
// current 5 1 2 3 4 6 Is the next smallest element
public void nextPermutation(int[] nums) {
int n = nums.length, i, j;
for (i = n - 2; i >= 0; i--) {
if (nums[i] < nums[i + 1]) {
break;
}
}
if (i == -1) {
Arrays.sort(nums);
return;
}
for (j = n - 1; j > i; j--) {
if (nums[j] > nums[i]) {
break;
}
}
var t = nums[i];
nums[i] = nums[j];
nums[j] = t;
Arrays.sort(nums, i + 1, n);
}
}
class Solution {
// Monotonic stack 10:12 10:30
// Looking back and forth , If it's in ascending order , The description cannot be changed later, for example 4 6 5 3 2 1, We cannot change 65321 From your location
// Find the next one
// however 4 6 It's in reverse order , Explain that you can find a comparison in the back 4 Large element exchange to make the exchange larger than before
// The first ratio found from the back 4 The big one is 5, In exchange for 4 and 5 And then get 5 6 4 3 2 1, But now it's not the smallest element
// It can only be said that 5 The element at the beginning must be larger than that just now 4 At the beginning , So we have to deal with 5 The following elements are arranged in order from small to large
// current 5 1 2 3 4 6 Is the next smallest element
public void nextPermutation(int[] nums) {
int n = nums.length, i, j;
for (i = n - 2; i >= 0; i--) {
if (nums[i] < nums[i + 1]) {
break;
}
}
if (i == -1) {
i = 0; j = n - 1;
while (i < j) {
int t = nums[i];
nums[i++] = nums[j];
nums[j--] = t;
}
return;
}
for (j = n - 1; j > i; j--) {
if (nums[j] > nums[i]) {
break;
}
}
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
i++;
j = n - 1;
while (i < j) {
t = nums[i];
nums[i++] = nums[j];
nums[j--] = t;
}
}
}
边栏推荐
- 编辑器-vi、vim的使用
- scipy. misc. imread()
- 交通运输部、教育部:广泛开展水上交通安全宣传和防溺水安全提醒
- Svg optimization by svgo
- Nips2021 | new SOTA for node classification beyond graphcl, gnn+ comparative learning
- nodejs_ fs. writeFile
- My life
- 阿里十年测试带你走进APP测试的世界
- Applet global style configuration window
- What is a firewall? Explanation of basic knowledge of firewall
猜你喜欢
[code practice] [stereo matching series] Classic ad census: (5) scan line optimization
[technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis
Svgo v3.9.0+
NIPS2021 | 超越GraphCL,GNN+对比学习的节点分类新SOTA
LeetCode 496. 下一个更大元素 I
[reading notes] Figure comparative learning gnn+cl
Understanding rotation matrix R from the perspective of base transformation
Applet customization component
OpenGL - Coordinate Systems
Applet global style configuration window
随机推荐
22-07-04 Xi'an Shanghao housing project experience summary (01)
Jenkins Pipeline 方法(函数)定义及调用
Causes and appropriate analysis of possible errors in seq2seq code of "hands on learning in depth"
Nodemon installation and use
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势
Kotlin introductory notes (II) a brief introduction to kotlin functions
What is a firewall? Explanation of basic knowledge of firewall
Driver's license physical examination hospital (114-2 hang up the corresponding hospital driver physical examination)
LeetCode 31. 下一个排列
2310. The number of bits is the sum of integers of K
太不好用了,长文章加图文,今后只写小短文
【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
OpenGL - Model Loading
Deep understanding of C language pointer
Applet data attribute method
High performance spark_ Transformation performance
深入浅出PyTorch中的nn.CrossEntropyLoss
Rebuild my 3D world [open source] [serialization-2]