17 lines
357 B
C++
17 lines
357 B
C++
#include "s0031_next_permutation.hpp"
|
|
|
|
void S0031::nextPermutation(vector<int>& nums) {
|
|
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());
|
|
}
|