问题
实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。
如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。
必须原地修改,只允许使用额外常数空间。
以下是一些例子,输入位于左侧列,其相应输出位于右侧列。
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
分析
注意到下一个排列总是比当前排列要大,除非该排列已经是最大的排列。我们希望找到一种方法,能够找到一个大于当前序列的新序列,且变大的幅度尽可能小。具体地:
我们需要将一个左边的「较小数」与一个右边的「较大数」交换,以能够让当前排列变大,从而得到下一个排列。
同时我们要让这个「较小数」尽量靠右,而「较大数」尽可能小。当交换完成后,「较大数」右边的数需要按照升序重新排列。这样可以在保证新排列大于原来排列的情况下,使变大的幅度尽可能小。
具体地,我们这样描述该算法,对于长度为 n 的排列 a:
首先从后向前查找第一个顺序对 (i,i+1),满足 a[i] < a[i+1]。这样「较小数」即为 a[i]。此时 [i+1,n)必然是下降序列。
如果找到了顺序对,那么在区间 [i+1,n)中从后向前查找第一个元素 jj满足 a[i] < a[j]。这样「较大数」即为 a[j]。
交换 a[i] 与 a[j],此时可以证明区间 [i+1,n) 必为降序。我们可以直接使用双指针反转区间 [i+1,n) 使其变为升序,而无需对该区间进行排序。
如果在步骤 1 找不到顺序对,说明当前序列已经是一个降序序列,即最大的序列,我们直接跳过步骤 2 执行步骤 3,即可得到最小的升序序列。
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
| class Solution { public void nextPermutation(int[] nums) { int smallIndex = -1, bigIndex = nums.length - 1; for (int i = nums.length - 2; i > -1; --i) { if (nums[i] < nums[i + 1]) { smallIndex = i; break; } } if (smallIndex == -1) { exchange(nums, 0, nums.length - 1); return ; } for (int i = nums.length - 1; i > smallIndex; --i) { if (nums[i] > nums[smallIndex]) { bigIndex = i; break; } } swap(nums, smallIndex, bigIndex); smallIndex++; bigIndex = nums.length - 1; exchange(nums, smallIndex, bigIndex); } private void swap(int[] nums, int smallIndex, int bigIndex) { int pre = nums[bigIndex]; nums[bigIndex] = nums[smallIndex]; nums[smallIndex] = pre; } private void exchange(int[] nums, int smallIndex, int bigIndex) { while (smallIndex < bigIndex) { swap(nums, smallIndex, bigIndex); smallIndex++; bigIndex--; } } }
|
思考:如果这个题改成上一个排列,代码该怎么改?
很简单,先找一个较大值,再找一个较小值,交换完再逆序排列。此时,较大值的下标小于较小值下标。
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
| class Solution { public void nextPermutation(int[] nums) { int smallIndex = -1, bigIndex = nums.length - 1; for (int i = nums.length - 2; i > -1; --i) { if (nums[i] > nums[i + 1]) { bigIndex = i; break; } } if (bigIndex == nums.length - 1) { exchange(nums, 0, nums.length - 1); return ; } for (int i = nums.length - 1; i > bigIndex; --i) { if (nums[i] < nums[bigIndex]) { smallIndex = i; break; } } swap(nums, bigIndex, smallIndex); bigIndex++; smallIndex = nums.length - 1; exchange(nums, bigIndex, smallIndex); } private void swap(int[] nums, int smallIndex, int bigIndex) { int pre = nums[bigIndex]; nums[bigIndex] = nums[smallIndex]; nums[smallIndex] = pre; } private void exchange(int[] nums, int smallIndex, int bigIndex) { while (smallIndex < bigIndex) { swap(nums, smallIndex, bigIndex); smallIndex++; bigIndex--; } } }
|