share my 13line c++ solution


  • 0
    H

    class Solution {
    public:
    void nextPermutation(vector<int>& nums) {
    int idx;
    for(idx = nums.size()-2; idx>=0; idx--){
    if(nums[idx] < nums[idx+1])
    break;
    }
    reverse(nums.begin()+idx+1, nums.end());
    if(idx >= 0){
    int i = idx;
    while(nums[++i] <= nums[idx]);
    swap(nums[i], nums[idx]);
    }
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.