(难度Medium) Problem 31. Next Permutation（下一个组合）

### 链接

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

Medium

### 题干

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 and use only constant 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

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

### 样例

1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

### 题解

#### 解法一： 暴力枚举

def brute_force(arr, ele, nums):
# 当所有元素都已经放入arr
if ele is None:
# 判断是否合法，并且是最小的
if arr > nums and arr < mini:
mini = arr
# 如果还有元素没有放入
for i in ele:
# 因为每次放入元素都会从备选当中移除，所以可以不用判断
arr.append(i)
ele.remove(i)
brute_force(arr, ele)
ele.append(i)