31.下一个排序

class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        int i, j;
        if(nums.size() <= 1)
            return;
        for(i = nums.size()-2; i >= 0; i--){   //找左标记
            if(nums[i] < nums[i + 1]){
                break;
            }
        }
        for(j = nums.size()-1; j > i && i != -1; j--){    //找右标记
            if(nums[i] < nums[j]){
                swap(nums[i], nums[j]);
                break;
            }
        }
        reverse(nums.begin() + i + 1, nums.end());
    }
};

Accepted
265/265 cases passed (4 ms)
Your runtime beats 72.39 % of cpp submissions
Your memory usage beats 94.62 % of cpp submissions (11.6 MB)

上一篇:最早的90后程序员已经31岁,最早的00后已进BATM大厂,你在哪里?


下一篇:洛谷 P2367 语文成绩