forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cs
37 lines (33 loc) · 956 Bytes
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
namespace LeetCodeNet.G0001_0100.S0031_next_permutation {
// #Medium #Top_100_Liked_Questions #Array #Two_Pointers #Big_O_Time_O(n)_Space_O(1)
// #2024_01_11_Time_98_ms_(94.17%)_Space_46_MB_(24.35%)
public class Solution {
public void NextPermutation(int[] nums) {
if (nums == null || nums.Length <= 1) {
return;
}
int i = nums.Length - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--;
}
if (i >= 0) {
int j = nums.Length - 1;
while (nums[j] <= nums[i]) {
j--;
}
Swap(nums, i, j);
}
Reverse(nums, i + 1, nums.Length - 1);
}
private void Swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
private void Reverse(int[] nums, int i, int j) {
while (i < j) {
Swap(nums, i++, j--);
}
}
}
}