mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 10:06:28 +08:00
Update the code of permutations I and II
This commit is contained in:
parent
3f430fb85e
commit
9c070a028f
3 changed files with 72 additions and 22 deletions
|
@ -7,11 +7,11 @@
|
|||
using hello_algo.utils;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class permutations_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) {
|
||||
res.Add(new List<int>(state));
|
||||
|
@ -22,24 +22,29 @@ public class permutations_i {
|
|||
int choice = choices[i];
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i]) {
|
||||
// 尝试
|
||||
selected[i] = true; // 做出选择
|
||||
state.Add(choice); // 更新状态
|
||||
// 尝试:做出选择,更新状态
|
||||
selected[i] = true;
|
||||
state.Add(choice);
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退
|
||||
selected[i] = false; // 撤销选择
|
||||
state.RemoveAt(state.Count - 1); // 恢复到之前的状态
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
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]
|
||||
public void Test() {
|
||||
int[] nums = { 1, 2, 3 };
|
||||
|
||||
// 回溯算法
|
||||
List<List<int>> res = new List<List<int>>();
|
||||
backtrack(new List<int>(), nums, new bool[nums.Length], res);
|
||||
List<List<int>> res = permutationsI(nums);
|
||||
|
||||
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums));
|
||||
Console.WriteLine("所有排列 res = ");
|
||||
|
|
|
@ -7,11 +7,11 @@
|
|||
using hello_algo.utils;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class permutations_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) {
|
||||
res.Add(new List<int>(state));
|
||||
|
@ -23,25 +23,30 @@ public class permutations_ii {
|
|||
int choice = choices[i];
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i] && !duplicated.Contains(choice)) {
|
||||
// 尝试
|
||||
// 尝试:做出选择,更新状态
|
||||
duplicated.Add(choice); // 记录选择过的元素值
|
||||
selected[i] = true; // 做出选择
|
||||
state.Add(choice); // 更新状态
|
||||
selected[i] = true;
|
||||
state.Add(choice);
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退
|
||||
selected[i] = false; // 撤销选择
|
||||
state.RemoveAt(state.Count - 1); // 恢复到之前的状态
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
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]
|
||||
public void Test() {
|
||||
int[] nums = { 1, 2, 2 };
|
||||
|
||||
// 回溯算法
|
||||
List<List<int>> res = new List<List<int>>();
|
||||
backtrack(new List<int>(), nums, new bool[nums.Length], res);
|
||||
List<List<int>> res = permutationsII(nums);
|
||||
|
||||
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums));
|
||||
Console.WriteLine("所有排列 res = ");
|
||||
|
|
|
@ -32,60 +32,80 @@
|
|||
|
||||
```java title="permutations_i.java"
|
||||
[class]{permutations_i}-[func]{backtrack}
|
||||
|
||||
[class]{permutations_i}-[func]{permutationsI}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="permutations_i.cpp"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="permutations_i.py"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutations_i}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="permutations_i.go"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```javascript title="permutations_i.js"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="permutations_i.ts"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="permutations_i.c"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="permutations_i.cs"
|
||||
[class]{permutations_i}-[func]{backtrack}
|
||||
|
||||
[class]{permutations_i}-[func]{permutationsI}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="permutations_i.swift"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="permutations_i.zig"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
```
|
||||
|
||||
需要重点关注的是,我们引入了一个布尔型数组 `selected` ,它的长度与输入数组长度相等,其中 `selected[i]` 表示 `choices[i]` 是否已被选择。我们利用 `selected` 避免某个元素被重复选择,从而实现剪枝。
|
||||
|
@ -114,60 +134,80 @@
|
|||
|
||||
```java title="permutations_ii.java"
|
||||
[class]{permutations_ii}-[func]{backtrack}
|
||||
|
||||
[class]{permutations_ii}-[func]{permutationsII}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="permutations_ii.cpp"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutationsII}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="permutations_ii.py"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutations_ii}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="permutations_ii.go"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutationsII}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```javascript title="permutations_ii.js"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutationsII}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="permutations_ii.ts"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutationsII}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="permutations_ii.c"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutationsII}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="permutations_ii.cs"
|
||||
[class]{permutations_ii}-[func]{backtrack}
|
||||
|
||||
[class]{permutations_ii}-[func]{permutationsII}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="permutations_ii.swift"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutationsII}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="permutations_ii.zig"
|
||||
[class]{}-[func]{backtrack}
|
||||
|
||||
[class]{}-[func]{permutationsII}
|
||||
```
|
||||
|
||||
注意,虽然 `selected` 和 `duplicated` 都起到剪枝的作用,但他们剪掉的是不同的分支:
|
||||
|
|
Loading…
Reference in a new issue