## [46\. 全排列](https://leetcode-cn.com/problems/permutations/)
>Medium
#### 思路
先在纸上推演一下这道题的解法:
```
#1 确定 1 :
#2 2 or 3
#3 3 or 2
#1 确定 2 :
#2 1 or 3
#3 3 or 1
#1 确定 3 :
#2 1 or 2
#3 2 or 1
```
暴力解法的for循环嵌套数量是根据数组长度来决定的,现在是不可行的。通过上面的演算过程,我们也能想到,应该使用递归在求解:先定下一个,打开后面的门,再定一个打开后面的门,打开后最后,然后在一扇一扇门回来。
**递归终止条件**我们使用一个数组在记录,现在选到了第几个数字,开到了哪一扇门,当这个数组长度等于我们的元素个数时结束,开到了最后一扇门。
以上,AC!
#### 代码
python3
```
class Solution:
def helper(self, nums, result, cur, i):
cur.append(i)
if len(cur) == len(nums):
result.append(cur)
for n in nums:
if n not in cur:
self.helper(nums,result,cur[::],n)
def permute(self, nums: List[int]) -> List[List[int]]:
result = []
for n in nums:
self.helper(nums, result, [], n)
return result
```
- 目录
- excel-sheet-column-number
- divide-two-integers
- house-robber
- fraction-to-recurring-decimal
- profile
- kids-with-the-greatest-number-of-candies
- qiu-12n-lcof
- new-21-game
- product-of-array-except-self
- minimum-depth-of-binary-tree
- univalued-binary-tree
- shun-shi-zhen-da-yin-ju-zhen-lcof
- permutations
- satisfiability-of-equality-equations
- word-ladder-ii
- ba-shu-zi-fan-yi-cheng-zi-fu-chuan-lcof
- palindrome-number
- network-delay-time
- daily-temperatures
- longest-common-prefix
- sum-of-mutated-array-closest-to-target
- 周赛专题
- make-two-arrays-equal-by-reversing-sub-arrays
- check-if-a-string-contains-all-binary-codes-of-size-k
- course-schedule-iv
- cherry-pickup-ii
- maximum-product-of-two-elements-in-an-array
- maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts
- reorder-routes-to-make-all-paths-lead-to-the-city-zero
- probability-of-a-two-boxes-having-the-same-number-of-distinct-balls
- shuffle-the-array
- the-k-strongest-values-in-an-array
- design-browser-history
- paint-house-iii
- final-prices-with-a-special-discount-in-a-shop