10000 0046_permutations · kimi0230/LeetcodeGolang@c7ab0bb · GitHub
[go: up one dir, main page]

Skip to content

Commit c7ab0bb

Browse files
committed
0046_permutations
1 parent 3b207b9 commit c7ab0bb

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Leetcode/0046.Permutations/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,33 @@ Given a collection of **distinct** integers, return all possible permutations(
1818
給定一個沒有重複數字的序列,返回其所有可能的全排列。
1919

2020
## 解題思路
21+
解決回朔問題可用一個`決策樹`的遍歷過程
22+
1. 路徑: 也就是已經做的選擇
23+
2. 選擇列表: 也就是當前可以做的選擇
24+
3. 結束條件: 也就是達到決策樹底層, 無法再做選擇的條件
25+
26+
```python
27+
result = []
28+
def backtrack(路徑, 選擇列表):
29+
if 滿足結束條件:
30+
result.add(路徑)
31+
return
32+
33+
for 選擇 in 選擇列表:
34+
做選擇
35+
backtrack(路徑, 選擇列表)
36+
撤銷選擇
37+
```
38+
```
39+
選擇:[1,2,3]
40+
[]
41+
[1]/ |[2] \[3]
42+
[2]/ \[3] [1]/ \[3] [1]/ \[2]
43+
|[3] |[2] |[3] |[1] |[2] |[1]
44+
結果 [1,2,3] [1,3,2] [2,1,3] [2,3,1] [3,1,2] [3,2,1]
45+
```
46+
![](/asset/images/0046_permutations.png)
47+
2148
- 求出一個數組的排列組合中的所有排列,用 DFS 深搜即可。
2249
這個問題可以看作有 ñ 個排列成一行的空格,我們需要從左往右依此填入題目給定的 ñ個數,每個數只能使用一次。
2350
那麼很直接的可以想到一種窮舉的算法,即從左往右每一個位置都依此嘗試填入一個數,
@@ -38,6 +65,8 @@ Given a collection of **distinct** integers, return all possible permutations(
3865
## 解答
3966
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0046.Permutations/Permutations.go
4067

68+
69+
時間複雜 O(n)
4170
```go
4271
package permutations
4372

asset/images/0046_permutations.png

79.1 KB
Loading

0 commit comments

Comments
 (0)
0