Leetcode31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place and use only constant extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
解题
设P是1~n的一个全排列: p = p 1 p 2 . . . . . . p n = p 1 p 2 . . . . . . p j − 1 p j p j + 1 . . . . . . p k − 1 p k p k + 1 . . . . . . p n p=p_1p_2......p_n=p_1p_2......p_{j-1}p_{j}p_{j+1}......p_{k-1}p_kp_{k+1}......p_{n} p=p1p2......pn=p1p2......pj−1pjpj+1......pk−1pkpk+1......pn
- 从排列的右端开始,找出第一个比右边相邻数字小的数字的序号
j
(j从左端开始计算),即 j = m a x i ∣ p i < p i + 1 j=max{i|p_i<p_i+1} j=maxi∣pi<pi+1 - 在 p j p_j pj的右边的数字中,找出所有比p_j大的数中最小的数字p_k,即 k=max{i|p_i>p_j}(右边的数从右至左是递增的,因此k是所有大于pj的数字中序号最大者)
- 对换 p i p_i pi, p k p_k pk
- 再将 p j + 1 . . . . . . p k − 1 p k p k + 1 . . . . . . p n p_{j+1}......p_{k-1}p_kp_{k+1}......p_n pj+1......pk−1pkpk+1......pn倒转得到排列 p ′ = p 1 p 2 . . . . . p j − 1 p j p n . . . . . p k + 1 p k p k − 1 . . . . . p j + 1 p'=p_1p_2.....p_{j-1}p_jp_n.....p_{k+1}p_kp_{k-1}.....p_{j+1} p′=p1p2.....pj−1pjpn.....pk+1pkpk−1.....pj+1,这就是排列p的下一个排列。
比如 nums = [1,2,7,4,3,1],下一个排列是什么?
我们找到第一个最大索引是 nums[1] = 2
再找到第二个最大索引是 nums[4] = 3
交换,nums = [1,3,7,4,2,1];
翻转,nums = [1,3,1,2,4,7].
- 时间复杂度:O(n),在最坏的情况下,只需要对整个数组进行两次扫描。
- 空间复杂度:O(1),没有使用额外的空间,原地替换足以做到。
Java
public class Solution {
public void nextPermutation(int[] nums) {
int i = nums.length - 2;//注意i的初始值
while (i >= 0 && nums[i + 1] <= nums[i]) {
i--;//从右往左找到比右相邻小的
}
if (i >= 0) {
int j = nums.length - 1;//注意j的初始值
while (j >= 0 && nums[j] <= nums[i]) {
j--;//从右往左找到第一个比nums[i]大的数nums[j]
}
swap(nums, i, j);//交换i,j
}
reverse(nums, i + 1);//交换[i+1,nums.length - 1]
}
private void reverse(int[] nums, int start) {//
int i = start, j = nums.length - 1;
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
Python
def nextPermutation(self, nums):
i = j = len(nums)-1
while i > 0 and nums[i-1] >= nums[i]:
i -= 1
if i == 0: # nums are in descending order
nums.reverse()
return
k = i - 1 # find the last "ascending" position
while nums[j] <= nums[k]:
j -= 1
nums[k], nums[j] = nums[j], nums[k]
l, r = k+1, len(nums)-1 # reverse the second part
while l < r:
nums[l], nums[r] = nums[r], nums[l]
l +=1 ; r -= 1
C++
- Find the largest index
k
such thatnums[k] < nums[k + 1]
. If no such index exists, just reverse nums and done. - Find the largest index
l > k
such thatnums[k] < nums[l]
. - Swap
nums[k]
andnums[l]
. - Reverse the sub-array
nums[k + 1:]
.
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int n = nums.size(), k, l;
for (k = n - 2; k >= 0; k--) {
if (nums[k] < nums[k + 1]) {
break;
}
}
if (k < 0) {
reverse(nums.begin(), nums.end());
} else {
for (l = n - 1; l > k; l--) {
if (nums[l] > nums[k]) {
break;
}
}
swap(nums[k], nums[l]);
reverse(nums.begin() + k + 1, nums.end());
}
}
};