c++ 9ms solution


  • 0
    X
    public:
        void nextPermutation(vector<int>& nums) {
            if(nums.size()<=1)
                return;
            for(int i=nums.size()-1; i>=1;--i){
                if(nums[i-1]<nums[i]){
                    int key=nums[i-1];
                    auto it=--find_if(nums.begin()+i,nums.end(),[key](int & a){return a<=key;});
                    swap(nums[i-1],*it);
                    sort(nums.begin()+i,nums.end());
                    return;
                }
            }
            sort(nums.begin(),nums.end());
            return;
        }
    

Log in to reply
 

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