My accepted c++ code


  • 0
    K
    viod nextPermutation(vector<int>& nums) {
            int len=nums.size();
            if(len==1)
            return;
            for(int i=len-1;;i--){
                if(nums[i]>nums[i-1]){
                    sort(nums.begin()+i,nums.end());
                    int k=i;
                    while(nums[i-1]>=nums[k]){
                        k++;
                    }
                    int temp=nums[k];
                    nums[k]=nums[i-1];
                    nums[i-1]=temp; 
                    sort(nums.begin()+i,nums.end());
                    break;
                    }
                else if(i==1){
                    sort(nums.begin(),nums.end());
                    break;
                    
                }
            }
        }

Log in to reply
 

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