当前位置:网站首页>LeetCode 31. 下一个排列
LeetCode 31. 下一个排列
2022-07-05 09:16:00 【Sasakihaise_】


【分析】如果想得到一个比当前数字大的元素,我们需要把后面一个较大的数字和前面一个较小的数字交换,并且前面被交换的位应该尽可能低,后面交换的数字尽可能小,但是得比前面大。那么就需要分两步来了:
1.找到前面要被交换的元素的位置。
2.从后往前找一个比他大的最小元素。
对于1,我们看一种情况5,4,3,2,1,我们发现此时这个数字已经是最大的排列了,也就是无法找到前面可以交换的位置。但是对于排列3,5,4,2,1,如果遮住3,后面的5,4,2,1也已经是极限最大了,但是3这个位置却还可以进行改变,于是不难发现,当一个序列按照逆序排列时,他组成的已经是最大值了。如果发现这样两个元素(i, j),nums[i] < nums[j],说明可以通过改变nums[i]来使得元素更大。这样我们就找到了前面要交换的元素的最小位了。
对于2,假设插入位置为i,那么[i + 1, n)一定是按照降序排列,继续从后往前遍历,寻找第一个比插入位置大的元素即可,这个元素就是大于i处元素的最小元素。
交换完成后,由于[i + 1, n)还是按照降序排列,他还是一个很大的数,但是由于交换i处的缘故,无论后面怎样排列,都比之前那个更大,为了使结果小,还要把这部分改成升序。(可以直接反转,也可以sort)
class Solution {
// 单调栈 10:12 10:30
// 从后往前看,如果是升序,说明后面已经无法改变了例如 4 6 5 3 2 1,我们无法通过改变65321的位置来
// 找下一个
// 但是4 6是逆序的,说明可以通过在后面找一个比4大的元素交换来使得交换后的比之前大
// 从后面找到的第一个比4大的是5,交换4和5之后得到 5 6 4 3 2 1,但是现在还不是最小的元素
// 只能说是以5开头的元素必定大于刚才比4开头的,所以还要对5后面的元素按照从小到大排个序
// 现在的 5 1 2 3 4 6才是下一个最小的元素
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 {
// 单调栈 10:12 10:30
// 从后往前看,如果是升序,说明后面已经无法改变了例如 4 6 5 3 2 1,我们无法通过改变65321的位置来
// 找下一个
// 但是4 6是逆序的,说明可以通过在后面找一个比4大的元素交换来使得交换后的比之前大
// 从后面找到的第一个比4大的是5,交换4和5之后得到 5 6 4 3 2 1,但是现在还不是最小的元素
// 只能说是以5开头的元素必定大于刚才比4开头的,所以还要对5后面的元素按照从小到大排个序
// 现在的 5 1 2 3 4 6才是下一个最小的元素
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;
}
}
}边栏推荐
- 【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
- C语言-从键盘输入数组二维数组a,将a中3×5矩阵中第3列的元素左移到第0列,第3列以后的每列元素行依次左移,原来左边的各列依次绕到右边
- Luo Gu p3177 tree coloring [deeply understand the cycle sequence of knapsack on tree]
- RT thread kernel quick start, kernel implementation and application development learning with notes
- [beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
- 2310. The number of bits is the sum of integers of K
- Understanding rotation matrix R from the perspective of base transformation
- 顶会论文看图对比学习(GNN+CL)研究趋势
- Transfer learning and domain adaptation
- Codeworks round 639 (Div. 2) cute new problem solution
猜你喜欢

Can't find the activitymainbinding class? The pit I stepped on when I just learned databinding

什么是防火墙?防火墙基础知识讲解

My experience from technology to product manager

Blogger article navigation (classified, real-time update, permanent top)

Kotlin introductory notes (V) classes and objects, inheritance, constructors

Introduction Guide to stereo vision (7): stereo matching

Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method

Using request headers to develop multi terminal applications

Applet customization component

Information and entropy, all you want to know is here
随机推荐
Introduction Guide to stereo vision (7): stereo matching
notepad++
Svg optimization by svgo
C#图像差异对比:图像相减(指针法、高速)
OpenFeign
Mengxin summary of LCs (longest identical subsequence) topics
我的一生.
C # image difference comparison: image subtraction (pointer method, high speed)
云计算技术热点
Kotlin introductory notes (III) kotlin program logic control (if, when)
Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]
Applet (subcontracting)
Kotlin introductory notes (VIII) collection and traversal
Uni app implements global variables
Applet global style configuration window
Kotlin introductory notes (IV) circular statements (simple explanation of while, for)
Use arm neon operation to improve memory copy speed
Wxss template syntax
Shutter uses overlay to realize global pop-up
nodejs_ 01_ fs. readFile