leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0031.cpp (462B)


      1 class Solution {
      2   public:
      3     void nextPermutation(vector<int> &nums) {
      4         int n = nums.size(), k, l;
      5         for (k = n - 2; k >= 0 && nums[k] >= nums[k + 1]; k--)
      6             ;
      7 
      8         if (k >= 0) {
      9             for (l = n - 1; l > k && nums[l] <= nums[k]; l--)
     10                 ;
     11             swap(nums[k], nums[l]);
     12             reverse(nums.begin() + k + 1, nums.end());
     13         } else {
     14             reverse(nums.begin(), nums.end());
     15         }
     16     }
     17 };