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
就是全排列,找下一个数。上图是比较具象的过程。
765431是已经排好的,这部分已经是最大了,找到 4 比 7小,则把4和765431中的仅比4大的5交换,然后在76431找到最小的数:13467
我在discuss里找到了什么????STL太牛逼了= =。。。。。
class Solution {
public:
void nextPermutation(vector<int>& nums) {
std::next_permutation(nums.begin(), nums.end());
}
};