Sharing my simple code using 12ms


  • 0
    S
    void nextPermutation(vector<int>& nums){	
    int i(nums.size());
    int j(i);
    while(--i>0 && nums[i] <= nums[i-1]);
        if (i) {//if i equals 0,show that there isn't a next permutation
    	while(--j>i-1 && nums[j] <= nums[i-1]);//find the element bigger than nums[i-1]
    	swap(nums[j],nums[i-1]);	
    	}
    	for (int m = i,n = nums.size()-1;m<n;m++,n-- )//exchange from i to nums.size()-1
        swap(nums[m],nums[n]);
    }

Log in to reply
 

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