Update the code of permutations I and II

pull/480/head
krahets 2 years ago
parent 3f430fb85e
commit 9c070a028f

@ -7,11 +7,11 @@
using hello_algo.utils; using hello_algo.utils;
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_backtracking; namespace hello_algo.chapter_backtracking;
public class permutations_i { public class permutations_i {
/* 回溯算法:全排列 I */ /* 回溯算法:全排列 I */
public static void backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) { static void backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
// 当状态长度等于元素数量时,记录解 // 当状态长度等于元素数量时,记录解
if (state.Count == choices.Length) { if (state.Count == choices.Length) {
res.Add(new List<int>(state)); res.Add(new List<int>(state));
@ -22,24 +22,29 @@ public class permutations_i {
int choice = choices[i]; int choice = choices[i];
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
if (!selected[i]) { if (!selected[i]) {
// 尝试 // 尝试:做出选择,更新状态
selected[i] = true; // 做出选择 selected[i] = true;
state.Add(choice); // 更新状态 state.Add(choice);
backtrack(state, choices, selected, res); backtrack(state, choices, selected, res);
// 回退 // 回退:撤销选择,恢复到之前的状态
selected[i] = false; // 撤销选择 selected[i] = false;
state.RemoveAt(state.Count - 1); // 恢复到之前的状态 state.RemoveAt(state.Count - 1);
} }
} }
} }
/* 全排列 I */
static List<List<int>> permutationsI(int[] nums) {
List<List<int>> res = new List<List<int>>();
backtrack(new List<int>(), nums, new bool[nums.Length], res);
return res;
}
[Test] [Test]
public void Test() { public void Test() {
int[] nums = { 1, 2, 3 }; int[] nums = { 1, 2, 3 };
// 回溯算法 List<List<int>> res = permutationsI(nums);
List<List<int>> res = new List<List<int>>();
backtrack(new List<int>(), nums, new bool[nums.Length], res);
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums)); Console.WriteLine("输入数组 nums = " + string.Join(", ", nums));
Console.WriteLine("所有排列 res = "); Console.WriteLine("所有排列 res = ");

@ -7,11 +7,11 @@
using hello_algo.utils; using hello_algo.utils;
using NUnit.Framework; using NUnit.Framework;
namespace hello_algo.chapter_backtracking; namespace hello_algo.chapter_backtracking;
public class permutations_ii { public class permutations_ii {
/* 回溯算法:全排列 II */ /* 回溯算法:全排列 II */
public static void backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) { static void backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
// 当状态长度等于元素数量时,记录解 // 当状态长度等于元素数量时,记录解
if (state.Count == choices.Length) { if (state.Count == choices.Length) {
res.Add(new List<int>(state)); res.Add(new List<int>(state));
@ -23,25 +23,30 @@ public class permutations_ii {
int choice = choices[i]; int choice = choices[i];
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
if (!selected[i] && !duplicated.Contains(choice)) { if (!selected[i] && !duplicated.Contains(choice)) {
// 尝试 // 尝试:做出选择,更新状态
duplicated.Add(choice); // 记录选择过的元素值 duplicated.Add(choice); // 记录选择过的元素值
selected[i] = true; // 做出选择 selected[i] = true;
state.Add(choice); // 更新状态 state.Add(choice);
backtrack(state, choices, selected, res); backtrack(state, choices, selected, res);
// 回退 // 回退:撤销选择,恢复到之前的状态
selected[i] = false; // 撤销选择 selected[i] = false;
state.RemoveAt(state.Count - 1); // 恢复到之前的状态 state.RemoveAt(state.Count - 1);
} }
} }
} }
/* 全排列 II */
static List<List<int>> permutationsII(int[] nums) {
List<List<int>> res = new List<List<int>>();
backtrack(new List<int>(), nums, new bool[nums.Length], res);
return res;
}
[Test] [Test]
public void Test() { public void Test() {
int[] nums = { 1, 2, 2 }; int[] nums = { 1, 2, 2 };
// 回溯算法 List<List<int>> res = permutationsII(nums);
List<List<int>> res = new List<List<int>>();
backtrack(new List<int>(), nums, new bool[nums.Length], res);
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums)); Console.WriteLine("输入数组 nums = " + string.Join(", ", nums));
Console.WriteLine("所有排列 res = "); Console.WriteLine("所有排列 res = ");

@ -32,60 +32,80 @@
```java title="permutations_i.java" ```java title="permutations_i.java"
[class]{permutations_i}-[func]{backtrack} [class]{permutations_i}-[func]{backtrack}
[class]{permutations_i}-[func]{permutationsI}
``` ```
=== "C++" === "C++"
```cpp title="permutations_i.cpp" ```cpp title="permutations_i.cpp"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutationsI}
``` ```
=== "Python" === "Python"
```python title="permutations_i.py" ```python title="permutations_i.py"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutations_i}
``` ```
=== "Go" === "Go"
```go title="permutations_i.go" ```go title="permutations_i.go"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutationsI}
``` ```
=== "JavaScript" === "JavaScript"
```javascript title="permutations_i.js" ```javascript title="permutations_i.js"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutationsI}
``` ```
=== "TypeScript" === "TypeScript"
```typescript title="permutations_i.ts" ```typescript title="permutations_i.ts"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutationsI}
``` ```
=== "C" === "C"
```c title="permutations_i.c" ```c title="permutations_i.c"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutationsI}
``` ```
=== "C#" === "C#"
```csharp title="permutations_i.cs" ```csharp title="permutations_i.cs"
[class]{permutations_i}-[func]{backtrack} [class]{permutations_i}-[func]{backtrack}
[class]{permutations_i}-[func]{permutationsI}
``` ```
=== "Swift" === "Swift"
```swift title="permutations_i.swift" ```swift title="permutations_i.swift"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutationsI}
``` ```
=== "Zig" === "Zig"
```zig title="permutations_i.zig" ```zig title="permutations_i.zig"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutationsI}
``` ```
需要重点关注的是,我们引入了一个布尔型数组 `selected` ,它的长度与输入数组长度相等,其中 `selected[i]` 表示 `choices[i]` 是否已被选择。我们利用 `selected` 避免某个元素被重复选择,从而实现剪枝。 需要重点关注的是,我们引入了一个布尔型数组 `selected` ,它的长度与输入数组长度相等,其中 `selected[i]` 表示 `choices[i]` 是否已被选择。我们利用 `selected` 避免某个元素被重复选择,从而实现剪枝。
@ -114,60 +134,80 @@
```java title="permutations_ii.java" ```java title="permutations_ii.java"
[class]{permutations_ii}-[func]{backtrack} [class]{permutations_ii}-[func]{backtrack}
[class]{permutations_ii}-[func]{permutationsII}
``` ```
=== "C++" === "C++"
```cpp title="permutations_ii.cpp" ```cpp title="permutations_ii.cpp"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutationsII}
``` ```
=== "Python" === "Python"
```python title="permutations_ii.py" ```python title="permutations_ii.py"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutations_ii}
``` ```
=== "Go" === "Go"
```go title="permutations_ii.go" ```go title="permutations_ii.go"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutationsII}
``` ```
=== "JavaScript" === "JavaScript"
```javascript title="permutations_ii.js" ```javascript title="permutations_ii.js"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutationsII}
``` ```
=== "TypeScript" === "TypeScript"
```typescript title="permutations_ii.ts" ```typescript title="permutations_ii.ts"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutationsII}
``` ```
=== "C" === "C"
```c title="permutations_ii.c" ```c title="permutations_ii.c"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutationsII}
``` ```
=== "C#" === "C#"
```csharp title="permutations_ii.cs" ```csharp title="permutations_ii.cs"
[class]{permutations_ii}-[func]{backtrack} [class]{permutations_ii}-[func]{backtrack}
[class]{permutations_ii}-[func]{permutationsII}
``` ```
=== "Swift" === "Swift"
```swift title="permutations_ii.swift" ```swift title="permutations_ii.swift"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutationsII}
``` ```
=== "Zig" === "Zig"
```zig title="permutations_ii.zig" ```zig title="permutations_ii.zig"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
[class]{}-[func]{permutationsII}
``` ```
注意,虽然 `selected``duplicated` 都起到剪枝的作用,但他们剪掉的是不同的分支: 注意,虽然 `selected``duplicated` 都起到剪枝的作用,但他们剪掉的是不同的分支:

Loading…
Cancel
Save