**Solution 1**

Just for info: There's a library function that does the job, even going from totally reverse sorted to sorted:

```
void nextPermutation(vector<int>& nums) {
next_permutation(begin(nums), end(nums));
}
```

**Solution 2**

Using library functions for all building blocks of the algorithm. Very nice how they all play together, notice the total lack of `+1`

/`-1`

, it all fits exactly.

```
void nextPermutation(vector<int>& nums) {
auto i = is_sorted_until(nums.rbegin(), nums.rend());
if (i != nums.rend())
swap(*i, *upper_bound(nums.rbegin(), i, *i));
reverse(nums.rbegin(), i);
}
```

**Solution 3**

Doing it all on my own (except `swap`

, let's not be silly):

```
void nextPermutation(vector<int>& nums) {
int i = nums.size() - 1, k = i;
while (i > 0 && nums[i-1] >= nums[i])
i--;
for (int j=i; j<k; j++, k--)
swap(nums[j], nums[k]);
if (i > 0) {
k = i--;
while (nums[k] <= nums[i])
k++;
swap(nums[i], nums[k]);
}
}
```

**Solution 4**

Ok, let's be silly after all and not even use `swap`

:-)

```
void nextPermutation(vector<int>& nums) {
int i = nums.size() - 1, k = i, tmp;
while (i > 0 && nums[i-1] >= nums[i])
i--;
for (int j=i; j<k; j++, k--)
tmp = nums[j], nums[j] = nums[k], nums[k] = tmp;
if (i > 0) {
k = i--;
while (nums[k] <= nums[i])
k++;
tmp = nums[i], nums[i] = nums[k], nums[k] = tmp;
}
}
```