代码:

 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. 感觉是纯粹的数学方法套用公式

[LeetCode] Next Permutation

 

相关文章:

  • 2021-05-25
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-23
  • 2021-07-07
  • 2021-09-24
  • 2021-12-17
  • 2021-09-15
  • 2021-09-29
相关资源
相似解决方案