1. 程式人生 > >leetcode---下一個排列

leetcode---下一個排列

實現獲取下一個排列的函式,演算法需要將給定數字序列重新排列成字典序中下一個更大的排列。

如果不存在下一個更大的排列,則將數字重新排列成最小的排列(即升序排列)。

必須原地修改,只允許使用額外常數空間。

以下是一些例子,輸入位於左側列,其相應輸出位於右側列。
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(!next_permutation(nums.begin(),nums.end()))
        sort(nums.begin(),nums.end());
        
    }
};