(1)从后往前,找到a[i]<a[i+1]
(2)从i后面找到最小的比a[i]大的元素,交换(i后面的元素都是递减的,所以实现就看自己的了)
(3)将i后的元素reverse一下即可
1 class Solution { 2 public: 3 void nextPermutation(vector<int> &num) { 4 int end = num.size() - 1; 5 int povit = end; 6 while(povit > 0){ 7 if(num[povit] > num[povit - 1]) break; 8 povit --; 9 } 10 if(povit > 0){ 11 povit --; 12 int large = end; 13 while(num[large] <= num[povit]) large --; 14 swap(num[large] , num[povit]); 15 reverse(num.begin() + povit + 1 , num.end()); 16 }else{ 17 reverse(num.begin() , num.end()); 18 } 19 } 20 };