# Next Permutation

carlblack

### 问题 Next Permutation

Difficulty: Medium

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, do not allocate 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`

``````class Solution {
public:
void nextPermutation(vector<int>& nums) {

}
};``````

### 思路

• abcd->abdc 最后两个交换

• abdc->acbd 先bc交换，再db交换

• acbd->acdb bd交换

• acdb->adbc 先cd交换， 再cb交换

规律有点难发现，其实在交换中我们下意识运用了规律(我是偷了别个的思想)。

1. 从右到左看，先找到第一个打破升序规律的字母c

2. 再从头来从右到左，找出c右边比c大的字母d（其实字典顺序他肯定在c右边），并把c右边的字母作为一个分区段

3. 交换c,d

4. 分区段的字母再按升序排列，这里由于就剩下c，升序排序后还是剩下c

### 代码

``````class Solution {
public:
void nextPermutation(vector<int>& nums) {
size_t len = nums.size();
if (len == 1 || len == 0){
return;
}
int firstBreakNum = -1;
size_t firstBreakIndex = -1;
for (size_t i = len ;i > 1; --i){
if (nums[i-1] > nums[i-2]){
firstBreakNum = nums[i-2];
firstBreakIndex = i - 2;
break;
}
}
// 没找到打破升序规律的，那就全部升序排列
if (firstBreakNum == -1){
reverse(nums.begin(), nums.end());
return;
}
for (size_t i = len; i > 1; --i){
if (nums[i-1] > firstBreakNum){
swap(nums[i-1], nums[firstBreakIndex]);
break;
}
}
auto iter = nums.begin();
for (size_t i = 0; i < firstBreakIndex + 1; ++i){
iter++;
}
reverse(iter, nums.end());
}
};``````

4 声望
16 粉丝
0 条评论