[LeetCode] Next Permutation
代码:
1 class Solution { 2 public: 3 void nextPermutation(vector<int> &num) { 4 5 const auto first = num.begin(); 6 const auto last = num.end(); 7 auto pivot = prev(last); 8 while (pivot != first && *prev(pivot) >= *pivot) { 9 pivot--; 10 } 11 if (pivot == first){ 12 reverse(first, last); 13 return; 14 } else 15 pivot--; 16 auto ppivot = prev(last); 17 while (*ppivot <= *pivot) { 18 ppivot--; 19 } 20 swap(*pivot, *ppivot); 21 reverse(++pivot, last); 22 return; 23 } 24 };
杂记:
1. 感觉是纯粹的数学方法套用公式