s0031
This commit is contained in:
parent
8901afccf4
commit
0872014b91
13
include/s0031_next_permutation.hpp
Normal file
13
include/s0031_next_permutation.hpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#ifndef S0031_NEXT_PERMUTATION
|
||||||
|
#define S0031_NEXT_PERMUTATION
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
void nextPermutation(vector<int>& nums);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
16
src/s0031_next_permutation.cpp
Normal file
16
src/s0031_next_permutation.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include "s0031_next_permutation.hpp"
|
||||||
|
|
||||||
|
void Solution::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());
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user