Skip to content

Commit 058850a

Browse files
committed
238th Commit
1 parent 9b164a1 commit 058850a

File tree

4 files changed

+76
-69
lines changed

4 files changed

+76
-69
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -468,15 +468,17 @@ Must-do List for Interview Prep
468468

469469
[208]: ./src/page-2/208.%20Implement%20Trie%20(Prefix%20Tree)/Trie.ts
470470

471-
| Backtracking | | |
472-
| ----------------------------------------- | -------- | ------ |
473-
| 17. Letter Combinations of a Phone Number | Solution | Medium |
474-
| 77. Combinations | Solution | Medium |
475-
| 46. Permutations | Solution | Medium |
476-
| 39. Combination Sum | Solution | Medium |
477-
| 52. N-Queens II | Solution | Hard |
478-
| 22. Generate Parentheses | Solution | Medium |
479-
| 79. Word Search | Solution | Medium |
471+
| Backtracking | | |
472+
| ----------------------------------------- | -------------- | ------ |
473+
| 17. Letter Combinations of a Phone Number | Solution | Medium |
474+
| 77. Combinations | Solution | Medium |
475+
| 46. Permutations | [Solution][46] | Medium |
476+
| 39. Combination Sum | Solution | Medium |
477+
| 52. N-Queens II | Solution | Hard |
478+
| 22. Generate Parentheses | Solution | Medium |
479+
| 79. Word Search | Solution | Medium |
480+
481+
[46]: ./src/page-1/46.%20Permutations/permute.ts
480482

481483
| Divide & Conquer | | |
482484
| ----------------------------------------------- | --------------- | ------ |
@@ -584,13 +586,14 @@ Must-do List for Interview Prep
584586
| 17. Letter Combinations of a Phone Number | [Solution][17] | Medium |
585587
| 22. Generate Parentheses | Solution | Medium |
586588
| 39. Combination Sum | Solution | Medium |
587-
| 46. Permutations | Solution | Medium |
589+
| 46. Permutations | [Solution][46] | Medium |
588590
| 51. N-Queens | Solution | Hard |
589591
| 78. Subsets | Solution | Medium |
590592
| 79. Word Search | Solution | Medium |
591593
| 131. Palindrome Partitioning | Solution | Medium |
592594

593595
[17]: ./src/page-1/17.%20Letter%20Combinations%20of%20a%20Phone%20Number/letterCombinations.ts
596+
[46]: ./src/page-1/46.%20Permutations/permute.ts
594597

595598
| Binary Search | | |
596599
| ----------------------------------------------------------- | -------------- | ------ |

algorithms/13.backtracking/README.md

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,65 +31,10 @@ flowchart TB
3131
2 -. 剪枝 .-> 6
3232
```
3333

34-
```ts
35-
[1, 2, 3];
36-
```
37-
38-
```ts
39-
[
40-
[1, 2, 3],
41-
[1, 3, 2],
42-
[2, 1, 3],
43-
[2, 3, 1],
44-
[3, 1, 2],
45-
[3, 2, 1],
46-
];
47-
```
48-
49-
```mermaid
50-
flowchart TB
51-
0(_, _, _)
52-
1(1, _, _)
53-
2(2, _, _)
54-
3(3, _, _)
55-
56-
4(1, 2, _)
57-
5(1, 3, _)
58-
6(2, 1, _)
59-
7(2, 3, _)
60-
8(3, 1, _)
61-
9(3, 2, _)
62-
63-
10(1, 2, 3)
64-
11(1, 3, 2)
65-
12(2, 1, 3)
66-
13(2, 3, 1)
67-
14(3, 1, 2)
68-
15(3, 2, 1)
69-
70-
0 --> 1
71-
0 --> 2
72-
0 --> 3
73-
74-
1 --> 4
75-
1 --> 5
76-
2 --> 6
77-
2 --> 7
78-
3 --> 8
79-
3 --> 9
80-
81-
4 --> 10
82-
5 --> 11
83-
6 --> 12
84-
7 --> 13
85-
8 --> 14
86-
9 --> 15
87-
```
88-
8934
```ts
9035
function permute(nums: number[]): number[][] {
9136
const result: number[][] = [];
92-
const used: boolean[] = Array(nums.length).fill(false);
37+
const selected: boolean[] = Array(nums.length).fill(false);
9338

9439
function backtrack(currentPermutation: number[]) {
9540
// 如果當前排列的長度等於原數組長度,說明我們已經生成了一個完整排列
@@ -101,18 +46,18 @@ function permute(nums: number[]): number[][] {
10146
// 走訪可用數字
10247
for (let i = 0; i < nums.length; i++) {
10348
// 如果該數字已經被選過,則跳過 (剪枝)
104-
if (used[i]) continue;
49+
if (selected[i]) continue;
10550

10651
// 做選擇
10752
currentPermutation.push(nums[i]);
108-
used[i] = true;
53+
selected[i] = true;
10954

11055
// 遞迴進行下一步選擇
11156
backtrack(currentPermutation);
11257

11358
// 撤銷選擇 (回溯)
11459
currentPermutation.pop();
115-
used[i] = false;
60+
selected[i] = false;
11661
}
11762
}
11863

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { permute } from './permute';
2+
3+
describe('46. Permutations', () => {
4+
test('permute', () => {
5+
expect(permute([1, 2, 3])).toStrictEqual([
6+
[1, 2, 3],
7+
[1, 3, 2],
8+
[2, 1, 3],
9+
[2, 3, 1],
10+
[3, 1, 2],
11+
[3, 2, 1],
12+
]);
13+
14+
expect(permute([0, 1])).toStrictEqual([
15+
[0, 1],
16+
[1, 0],
17+
]);
18+
19+
expect(permute([1])).toStrictEqual([[1]]);
20+
});
21+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
type Permute = (nums: number[]) => number[][];
2+
3+
/**
4+
* Accepted
5+
*/
6+
export const permute: Permute = (nums) => {
7+
const result: number[][] = [];
8+
const selected: boolean[] = Array(nums.length).fill(false);
9+
10+
function backtrack(currentPermutation: number[]) {
11+
// If the current permutation's length equals the input array's length, we have a complete permutation
12+
if (currentPermutation.length === nums.length) {
13+
result.push([...currentPermutation]);
14+
return;
15+
}
16+
17+
// Traverse the available numbers
18+
for (let i = 0; i < nums.length; i++) {
19+
// Skip if the number has already been selected (pruning)
20+
if (selected[i]) continue;
21+
22+
// Make a choice
23+
currentPermutation.push(nums[i]);
24+
selected[i] = true;
25+
26+
// Recursively make the next choice
27+
backtrack(currentPermutation);
28+
29+
// Undo the choice (backtrack)
30+
currentPermutation.pop();
31+
selected[i] = false;
32+
}
33+
}
34+
35+
backtrack([]); // Start backtracking
36+
37+
return result;
38+
};

0 commit comments

Comments
 (0)