整数数组的一个 排列 就是将其所有成员以序列或线性顺序排列。
例如,arr = [1,2,3] ,以下这些都可以视作 arr 的排列:[1,2,3]、[1,3,2]、[3,1,2]、[2,3,1] 。
整数数组的 下一个排列 是指其整数的下一个字典序更大的排列。更正式地,如果数组的所有排列根据其字典顺序从小到大排列在一个容器中,那么数组的 下一个排列 就是在这个有序容器中排在它后面的那个排列。如果不存在下一个更大的排列,那么这个数组必须重排为字典序最小的排列(即,其元素按升序排列)。
例如,arr = [1,2,3] 的下一个排列是 [1,3,2] 。
类似地,arr = [2,3,1] 的下一个排列是 [3,1,2] 。
而 arr = [3,2,1] 的下一个排列是 [1,2,3] ,因为 [3,2,1] 不存在一个字典序更大的排列。
给你一个整数数组 nums ,找出 nums 的下一个排列。
必须 原地 修改,只允许使用额外常数空间。
示例 1:
输入:nums = [1,2,3]
输出:[1,3,2]
示例 2:
输入:nums = [3,2,1]
输出:[1,2,3]
示例 3:
输入:nums = [1,1,5]
输出:[1,5,1]
提示:
1 <= nums.length <= 100
0 <= nums[i] <= 100
1、首先能想到的就是将 高位小的数字 与 低位大的数字 进行交换 ,这样就能使数字增大 (高位小的数字和低位大的数字是相对而言的,高位在左,低位在右。如:1 2,1为高位,2为低位)。
当我们在挑选 高位小的数字 和 低位大的数字数字时尽量挑选位权小的数字(靠左侧的数字),这样能保证交换后的数字增大,且增大数值较小。其次考虑到 高位小的数字 与 低位大的数字 进行交换 之后低位大的数字到了高位小的数字上,那么交换后低位数字的右侧无论怎样其都会变大。所以将交换后低位数字的右侧数字进行排序(从小到大)。
例: nums=[ 6 , 5 , 2 , 4 , 3 , 1 ]
如果数字从高位到低位 递减,则不存在一个更大的排列。 如 321, 翻转输出123
2、复杂度分析:
① 时间复杂度:O(n),n代表nums中元素的个数,遍历两边数组分别查找 高位小的数字 与 低位大的数字。再采用转置将交换后低位数字的右侧数字进行排序(从小到大)。
② 空间复杂度:O(1)。
class Solution1 {
public:
void nextPermutation(vector<int> &nums) {
if (nums.size() <= 1) return; // 如果数组长度小于等于1,不需要做任何操作
int firstDecreasingIndex = -1, nextGreaterIndex = -1;
// 找到第一个 nums[i-1] < nums[i] 的位置
for (int i = nums.size() - 1; i > 0; i--) {
if (nums[i - 1] < nums[i]) {
firstDecreasingIndex = i - 1;
break;
}
}
// 如果没有找到,说明已经是最大排列,直接反转数组
if (firstDecreasingIndex == -1) {
reverse(nums.begin(), nums.end());
return;
}
// 找到第一个大于 nums[firstDecreasingIndex] 的位置
for (int i = nums.size() - 1; i >= 0; i--) {
if (nums[i] > nums[firstDecreasingIndex]) {
nextGreaterIndex = i;
break;
}
}
// 交换两个元素
swap(nums[nextGreaterIndex], nums[firstDecreasingIndex]);
// 将 firstDecreasingIndex 后面的部分反转(交换后,第一个交换元素的右侧已经有序)
reverse(nums.begin() + firstDecreasingIndex + 1, nums.end());
}
};
class Solution2 {
public:
void nextPermutation(vector<int>& nums) {
// 从倒数第二个元素开始,寻找第一个下降的元素(nums[i] < nums[i + 1])
int i = nums.size() - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--; // 如果当前位置的数大于等于下一个数,继续向左寻找
}
// 如果 i >= 0,说明我们找到了一个下降的位置
if (i >= 0) {
// 从数组的末尾开始,找到第一个比 nums[i] 大的元素
int j = nums.size() - 1;
while (j >= 0 && nums[i] >= nums[j]) {
j--; // 如果当前位置的数小于等于 nums[i],继续向左寻找
}
// 交换 nums[i] 和 nums[j],使得局部排列变大
swap(nums[i], nums[j]);
}
// 反转 i 后面的所有元素,使得它们是升序排列,从而得到下一个最小的排列
reverse(nums.begin() + i + 1, nums.end());
}
};
#include
#include
#include
using namespace std;
class Solution1 {
public:
void nextPermutation(vector<int> &nums) {
if (nums.size() <= 1) return; // 如果数组长度小于等于1,不需要做任何操作
int firstDecreasingIndex = -1, nextGreaterIndex = -1;
// 找到第一个 nums[i-1] < nums[i] 的位置
for (int i = nums.size() - 1; i > 0; i--) {
if (nums[i - 1] < nums[i]) {
firstDecreasingIndex = i - 1;
break;
}
}
// 如果没有找到,说明已经是最大排列,直接反转数组
if (firstDecreasingIndex == -1) {
reverse(nums.begin(), nums.end());
return;
}
// 找到第一个大于 nums[firstDecreasingIndex] 的位置
for (int i = nums.size() - 1; i >= 0; i--) {
if (nums[i] > nums[firstDecreasingIndex]) {
nextGreaterIndex = i;
break;
}
}
// 交换两个元素
swap(nums[nextGreaterIndex], nums[firstDecreasingIndex]);
// 将 firstDecreasingIndex 后面的部分反转(交换后,第一个交换元素的右侧已经有序)
reverse(nums.begin() + firstDecreasingIndex + 1, nums.end());
}
};
int main(int argc, char const *argv[])
{
vector<int> nums={1,2,3};
Solution1 s;
s.nextPermutation(nums);
for(auto const &i:nums){
cout<<i<<" ";
}
return 0;
}
LeetCode 热题 100_下一个排列(99_31)原题链接
欢迎大家和我沟通交流(✿◠‿◠)