## [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