Skip to content

Commit 40e8e3d

Browse files
committed
三刷31
1 parent 079f82a commit 40e8e3d

File tree

3 files changed

+70
-10
lines changed

3 files changed

+70
-10
lines changed

docs/0031-next-permutation.adoc

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
[#0031-next-permutation]
22
= 31. 下一个排列
33

4-
https://leetcode.cn/problems/next-permutation/[LeetCode - 31. 下一个排列 ^]
4+
https://leetcode.cn/problems/next-permutation/[LeetCode - 31. 下一个排列^]
55

66
整数数组的一个 *排列* 就是将其所有成员以序列或线性顺序排列。
77

8-
* 例如,`+arr = [1,2,3]+` ,以下这些都可以视作 `+arr+` 的排列:`+[1,2,3]+``+[1,3,2]+``+[3,1,2]+``+[2,3,1]+`
8+
* 例如,`arr = [1,2,3]` ,以下这些都可以视作 `arr` 的排列:`[1,2,3]``[1,3,2]``[3,1,2]``[2,3,1]`
99
1010
整数数组的 *下一个排列* 是指其整数的下一个字典序更大的排列。更正式地,如果数组的所有排列根据其字典顺序从小到大排列在一个容器中,那么数组的 *下一个排列* 就是在这个有序容器中排在它后面的那个排列。如果不存在下一个更大的排列,那么这个数组必须重排为字典序最小的排列(即,其元素按升序排列)。
1111

12-
* 例如,`+arr = [1,2,3]+` 的下一个排列是 `+[1,3,2]+`
13-
* 类似地,`+arr = [2,3,1]+` 的下一个排列是 `+[3,1,2]+`
14-
* 而 `+arr = [3,2,1]+` 的下一个排列是 `+[1,2,3]+` ,因为 `+[3,2,1]+`
15-
不存在一个字典序更大的排列。
12+
* 例如,`arr = [1,2,3]` 的下一个排列是 `[1,3,2]`
13+
* 类似地,`arr = [2,3,1]` 的下一个排列是 `[3,1,2]`
14+
* 而 `arr = [3,2,1]` 的下一个排列是 `[1,2,3]` ,因为 `[3,2,1]` 不存在一个字典序更大的排列。
1615
17-
给你一个整数数组 `+nums+` ,找出 `+nums+` 的下一个排列。
16+
给你一个整数数组 `nums` ,找出 `nums` 的下一个排列。
1817

1918
必须 *https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95[原地]* 修改,只允许使用额外常数空间。
2019

21-
2220
*示例 1:*
2321

2422
....
@@ -42,8 +40,8 @@ https://leetcode.cn/problems/next-permutation/[LeetCode - 31. 下一个排列 ^]
4240

4341
*提示:*
4442

45-
* `+1 <= nums.length <= 100+`
46-
* `+0 <= nums[i] <= 100+`
43+
* `1 \<= nums.length \<= 100`
44+
* `0 \<= nums[i] \<= 100`
4745
4846
4947
== 思路分析
@@ -90,6 +88,15 @@ include::{sourcedir}/_0031_NextPermutation.java[tag=answer]
9088
include::{sourcedir}/_0031_NextPermutation_2.java[tag=answer]
9189
----
9290
--
91+
92+
三刷::
93+
+
94+
--
95+
[{java_src_attr}]
96+
----
97+
include::{sourcedir}/_0031_NextPermutation_3.java[tag=answer]
98+
----
99+
--
93100
====
94101

95102

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,11 @@ endif::[]
20192019
|{doc_base_url}/0032-longest-valid-parentheses.adoc[题解]
20202020
|❌ 栈。使用栈记录左右括号的下标,遇到左括号入栈,遇到右括号则出栈,计算当前节点到栈顶元素的距离,取最大值。在起始位置使用 `-1` 做哨兵;在“非法”右括号时,将哨兵替换成当前坐标。
20212021

2022+
|{counter:codes2503}
2023+
|{leetcode_base_url}/next-permutation/[31. 下一个排列^]
2024+
|{doc_base_url}/0031-next-permutation.adoc[题解]
2025+
|❌ 双指针。看了解题思路,自己写出来的。解题思路还要多推敲。
2026+
20222027
|===
20232028

20242029
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.diguage.algo.leetcode;
2+
3+
4+
public class _0031_NextPermutation_3 {
5+
// tag::answer[]
6+
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-11-30 18:46:38
10+
*/
11+
public void nextPermutation(int[] nums) {
12+
int index = nums.length - 2;
13+
while (index >= 0) {
14+
if (nums[index] < nums[index + 1]) {
15+
break;
16+
}
17+
index--;
18+
}
19+
if (index == -1) {
20+
reverse(nums, 0, nums.length - 1);
21+
return;
22+
}
23+
int big = nums.length - 1;
24+
while (big > index) {
25+
if (nums[big] > nums[index]) {
26+
break;
27+
}
28+
big--;
29+
}
30+
swap(nums, index, big);
31+
reverse(nums, index + 1, nums.length - 1);
32+
}
33+
34+
private void reverse(int[] nums, int start, int end) {
35+
while (start < end) {
36+
swap(nums, start, end);
37+
start++;
38+
end--;
39+
}
40+
}
41+
42+
private static void swap(int[] nums, int index, int big) {
43+
int temp = nums[index];
44+
nums[index] = nums[big];
45+
nums[big] = temp;
46+
}
47+
// end::answer[]
48+
}

0 commit comments

Comments
 (0)