https://www.cnblogs.com/eudiwffe/p/6260699.html
我觉得它写的挺好的,STL里面确实有permutation的,而且我觉得写的真的很好
permutations后面还有一道题,求全排序的,呜完全可以用STL里的方法来做。
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
class Solution {
public:
void nextPermutation(vector<int>& nums) {
if(nums.size() <= 1)
return;
int i = nums.size() - 1, len = i;
while( i > 0 && nums[i-1] >= nums[i]) //找到第一个前面一个数比后面的数小的
i--;
if(i > 0){ //与从后往前数第一个比它大的数交换
int temp = i - 1;
int k = len;
while(nums[k] <= nums[temp] )
k--;
swap(nums[k], nums[temp]);
}
for(int j = i; j < len; j++, len--) //reverse后面的序列
swap(nums[j], nums[len]);
}
};