leetcode/src/s0031_next_permutation.cpp

17 lines
357 B
C++
Raw Normal View History

2022-11-21 06:45:25 +00:00
#include "s0031_next_permutation.hpp"
2022-11-30 10:20:36 +00:00
void S0031::nextPermutation(vector<int>& nums) {
2022-11-21 06:45:25 +00:00
int i = nums.size() - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--;
}
if (i >= 0) {
int j = nums.size() - 1;
while (j >= 0 && nums[i] >= nums[j]) {
j--;
}
swap(nums[i], nums[j]);
}
reverse(nums.begin() + i + 1, nums.end());
}