# LeetCode 31. Next Permutation

Problem:

https://leetcode.com/problems/next-permutation/

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
`1,2,3` → `1,3,2`
`3,2,1` → `1,2,3`
`1,1,5` → `1,5,1`

Thought:

from end to begin, find the first number that have number greater than it after, then swap the number with the minimum greater number, the sort the array after the number.

e.g    2 6 3 4 3 1     find  arr = 3, the swap it with arr = 4, sort arr to arr

Code  C++:

```class Solution {
public:
void nextPermutation(vector<int>& nums) {
if (nums.size() <= 1)
return;

for (int i = nums.size() - 2; i >= 0; i--) {
int greater_min = i;//greater_min point to the minimum number greater than nums[i]

for (int j = nums.size() - 1; j > i; j--) {//get greater_min
if (nums[j] > nums[i]) {
if (greater_min == i) {
greater_min = j;
continue;
}
greater_min  = nums[j] < nums[greater_min] ? j : greater_min;
}
}

if (greater_min != i) {
swap(nums[i], nums[greater_min]);
sort(nums.begin() + i + 1, nums.end());
return;
}
}
sort(nums.begin(), nums.end());
return;
}
};```

## [array] leetcode - 31. Next Permutation - Medium

leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

## LeetCode 31. Next Permutation （下一个排列）

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replaceme

## leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

Next Permutation Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending orde

## [LeetCode] 31. Next Permutation ☆☆☆

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replaceme

## LeetCode 31 Next Permutation (C,C++,Java,Python)

Problem: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The

## [leetcode]31. Next Permutation 下一个排列

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replaceme