-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21df0d3
commit 9b164a1
Showing
4 changed files
with
185 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,48 @@ | ||
# 分治 (Divide and Conquer) | ||
|
||
- 分 (劃分) | ||
- 治 (合併) | ||
|
||
```ts | ||
[8, 4, 2, 6]; | ||
``` | ||
|
||
分: | ||
|
||
```mermaid | ||
flowchart TB | ||
0(8, 4, 2, 6) | ||
1(8, 4) | ||
2(2, 6) | ||
3(8) | ||
4(4) | ||
5(2) | ||
6(6) | ||
0 --> 1 | ||
0 --> 2 | ||
1 --> 3 | ||
1 --> 4 | ||
2 --> 5 | ||
2 --> 6 | ||
``` | ||
|
||
治: | ||
|
||
```mermaid | ||
flowchart TB | ||
0(8) | ||
1(4) | ||
2(2) | ||
3(6) | ||
4(4, 8) | ||
5(2, 6) | ||
6(2, 4, 6, 8) | ||
0 --> 4 | ||
1 --> 4 | ||
2 --> 5 | ||
3 --> 5 | ||
4 --> 6 | ||
5 --> 6 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,123 @@ | ||
# 回溯 (Backtracking) | ||
|
||
### 全排列 (Permutations) | ||
|
||
```ts | ||
[1, 2]; | ||
``` | ||
|
||
```ts | ||
[ | ||
[1, 2], | ||
[2, 1], | ||
]; | ||
``` | ||
|
||
```mermaid | ||
flowchart TB | ||
0(_, _) | ||
1(1, _) | ||
2(2, _) | ||
3(1, 1) | ||
4(1, 2) | ||
5(2, 1) | ||
6(2, 2) | ||
0 --> 1 | ||
0 --> 2 | ||
1 -. 剪枝 .-> 3 | ||
1 --> 4 | ||
2 --> 5 | ||
2 -. 剪枝 .-> 6 | ||
``` | ||
|
||
```ts | ||
[1, 2, 3]; | ||
``` | ||
|
||
```ts | ||
[ | ||
[1, 2, 3], | ||
[1, 3, 2], | ||
[2, 1, 3], | ||
[2, 3, 1], | ||
[3, 1, 2], | ||
[3, 2, 1], | ||
]; | ||
``` | ||
|
||
```mermaid | ||
flowchart TB | ||
0(_, _, _) | ||
1(1, _, _) | ||
2(2, _, _) | ||
3(3, _, _) | ||
4(1, 2, _) | ||
5(1, 3, _) | ||
6(2, 1, _) | ||
7(2, 3, _) | ||
8(3, 1, _) | ||
9(3, 2, _) | ||
10(1, 2, 3) | ||
11(1, 3, 2) | ||
12(2, 1, 3) | ||
13(2, 3, 1) | ||
14(3, 1, 2) | ||
15(3, 2, 1) | ||
0 --> 1 | ||
0 --> 2 | ||
0 --> 3 | ||
1 --> 4 | ||
1 --> 5 | ||
2 --> 6 | ||
2 --> 7 | ||
3 --> 8 | ||
3 --> 9 | ||
4 --> 10 | ||
5 --> 11 | ||
6 --> 12 | ||
7 --> 13 | ||
8 --> 14 | ||
9 --> 15 | ||
``` | ||
|
||
```ts | ||
function permute(nums: number[]): number[][] { | ||
const result: number[][] = []; | ||
const used: boolean[] = Array(nums.length).fill(false); | ||
|
||
function backtrack(currentPermutation: number[]) { | ||
// 如果當前排列的長度等於原數組長度,說明我們已經生成了一個完整排列 | ||
if (currentPermutation.length === nums.length) { | ||
result.push([...currentPermutation]); | ||
return; | ||
} | ||
|
||
// 走訪可用數字 | ||
for (let i = 0; i < nums.length; i++) { | ||
// 如果該數字已經被選過,則跳過 (剪枝) | ||
if (used[i]) continue; | ||
|
||
// 做選擇 | ||
currentPermutation.push(nums[i]); | ||
used[i] = true; | ||
|
||
// 遞迴進行下一步選擇 | ||
backtrack(currentPermutation); | ||
|
||
// 撤銷選擇 (回溯) | ||
currentPermutation.pop(); | ||
used[i] = false; | ||
} | ||
} | ||
|
||
backtrack([]); // 開始回溯 | ||
|
||
return result; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters