mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-09 14:06:06 +00:00
build
This commit is contained in:
@@ -58,89 +58,411 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="permutations_i.py"
|
||||
[class]{}-[func]{backtrack}
|
||||
def backtrack(
|
||||
state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]
|
||||
):
|
||||
"""回溯算法:全排列 I"""
|
||||
# 当状态长度等于元素数量时,记录解
|
||||
if len(state) == len(choices):
|
||||
res.append(list(state))
|
||||
return
|
||||
# 遍历所有选择
|
||||
for i, choice in enumerate(choices):
|
||||
# 剪枝:不允许重复选择元素
|
||||
if not selected[i]:
|
||||
# 尝试:做出选择,更新状态
|
||||
selected[i] = True
|
||||
state.append(choice)
|
||||
# 进行下一轮选择
|
||||
backtrack(state, choices, selected, res)
|
||||
# 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = False
|
||||
state.pop()
|
||||
|
||||
[class]{}-[func]{permutations_i}
|
||||
def permutations_i(nums: list[int]) -> list[list[int]]:
|
||||
"""全排列 I"""
|
||||
res = []
|
||||
backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)
|
||||
return res
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="permutations_i.cpp"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 I */
|
||||
void backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.size() == choices.size()) {
|
||||
res.push_back(state);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (int i = 0; i < choices.size(); i++) {
|
||||
int choice = choices[i];
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i]) {
|
||||
// 尝试:做出选择,更新状态
|
||||
selected[i] = true;
|
||||
state.push_back(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.pop_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
/* 全排列 I */
|
||||
vector<vector<int>> permutationsI(vector<int> nums) {
|
||||
vector<int> state;
|
||||
vector<bool> selected(nums.size(), false);
|
||||
vector<vector<int>> res;
|
||||
backtrack(state, nums, selected, res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="permutations_i.java"
|
||||
[class]{permutations_i}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 I */
|
||||
void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.size() == choices.length) {
|
||||
res.add(new ArrayList<Integer>(state));
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (int i = 0; i < choices.length; i++) {
|
||||
int choice = choices[i];
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i]) {
|
||||
// 尝试:做出选择,更新状态
|
||||
selected[i] = true;
|
||||
state.add(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{permutations_i}-[func]{permutationsI}
|
||||
/* 全排列 I */
|
||||
List<List<Integer>> permutationsI(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<List<Integer>>();
|
||||
backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="permutations_i.cs"
|
||||
[class]{permutations_i}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 I */
|
||||
void backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.Count == choices.Length) {
|
||||
res.Add(new List<int>(state));
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (int i = 0; i < choices.Length; i++) {
|
||||
int choice = choices[i];
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i]) {
|
||||
// 尝试:做出选择,更新状态
|
||||
selected[i] = true;
|
||||
state.Add(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.RemoveAt(state.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{permutations_i}-[func]{permutationsI}
|
||||
/* 全排列 I */
|
||||
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;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="permutations_i.go"
|
||||
[class]{}-[func]{backtrackI}
|
||||
/* 回溯算法:全排列 I */
|
||||
func backtrackI(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if len(*state) == len(*choices) {
|
||||
newState := append([]int{}, *state...)
|
||||
*res = append(*res, newState)
|
||||
}
|
||||
// 遍历所有选择
|
||||
for i := 0; i < len(*choices); i++ {
|
||||
choice := (*choices)[i]
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if !(*selected)[i] {
|
||||
// 尝试:做出选择,更新状态
|
||||
(*selected)[i] = true
|
||||
*state = append(*state, choice)
|
||||
// 进行下一轮选择
|
||||
backtrackI(state, choices, selected, res)
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
(*selected)[i] = false
|
||||
*state = (*state)[:len(*state)-1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
/* 全排列 I */
|
||||
func permutationsI(nums []int) [][]int {
|
||||
res := make([][]int, 0)
|
||||
state := make([]int, 0)
|
||||
selected := make([]bool, len(nums))
|
||||
backtrackI(&state, &nums, &selected, &res)
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="permutations_i.swift"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 I */
|
||||
func backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if state.count == choices.count {
|
||||
res.append(state)
|
||||
return
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (i, choice) in choices.enumerated() {
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if !selected[i] {
|
||||
// 尝试:做出选择,更新状态
|
||||
selected[i] = true
|
||||
state.append(choice)
|
||||
// 进行下一轮选择
|
||||
backtrack(state: &state, choices: choices, selected: &selected, res: &res)
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false
|
||||
state.removeLast()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
/* 全排列 I */
|
||||
func permutationsI(nums: [Int]) -> [[Int]] {
|
||||
var state: [Int] = []
|
||||
var selected = Array(repeating: false, count: nums.count)
|
||||
var res: [[Int]] = []
|
||||
backtrack(state: &state, choices: nums, selected: &selected, res: &res)
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="permutations_i.js"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 I */
|
||||
function backtrack(state, choices, selected, res) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.length === choices.length) {
|
||||
res.push([...state]);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
choices.forEach((choice, i) => {
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i]) {
|
||||
// 尝试:做出选择,更新状态
|
||||
selected[i] = true;
|
||||
state.push(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.pop();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
/* 全排列 I */
|
||||
function permutationsI(nums) {
|
||||
const res = [];
|
||||
backtrack([], nums, Array(nums.length).fill(false), res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="permutations_i.ts"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 I */
|
||||
function backtrack(
|
||||
state: number[],
|
||||
choices: number[],
|
||||
selected: boolean[],
|
||||
res: number[][]
|
||||
): void {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.length === choices.length) {
|
||||
res.push([...state]);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
choices.forEach((choice, i) => {
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i]) {
|
||||
// 尝试:做出选择,更新状态
|
||||
selected[i] = true;
|
||||
state.push(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.pop();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
/* 全排列 I */
|
||||
function permutationsI(nums: number[]): number[][] {
|
||||
const res: number[][] = [];
|
||||
backtrack([], nums, Array(nums.length).fill(false), res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="permutations_i.dart"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 I */
|
||||
void backtrack(
|
||||
List<int> state,
|
||||
List<int> choices,
|
||||
List<bool> selected,
|
||||
List<List<int>> res,
|
||||
) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.length == choices.length) {
|
||||
res.add(List.from(state));
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (int i = 0; i < choices.length; i++) {
|
||||
int choice = choices[i];
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i]) {
|
||||
// 尝试:做出选择,更新状态
|
||||
selected[i] = true;
|
||||
state.add(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.removeLast();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
/* 全排列 I */
|
||||
List<List<int>> permutationsI(List<int> nums) {
|
||||
List<List<int>> res = [];
|
||||
backtrack([], nums, List.filled(nums.length, false), res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="permutations_i.rs"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 I */
|
||||
fn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if state.len() == choices.len() {
|
||||
res.push(state);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for i in 0..choices.len() {
|
||||
let choice = choices[i];
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if !selected[i] {
|
||||
// 尝试:做出选择,更新状态
|
||||
selected[i] = true;
|
||||
state.push(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state.clone(), choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.remove(state.len() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutations_i}
|
||||
/* 全排列 I */
|
||||
fn permutations_i(nums: &mut [i32]) -> Vec<Vec<i32>> {
|
||||
let mut res = Vec::new(); // 状态(子集)
|
||||
backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);
|
||||
res
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="permutations_i.c"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 I */
|
||||
void backtrack(vector *state, vector *choices, vector *selected, vector *res) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state->size == choices->size) {
|
||||
vector *newState = newVector();
|
||||
for (int i = 0; i < state->size; i++) {
|
||||
vectorPushback(newState, state->data[i], sizeof(int));
|
||||
}
|
||||
vectorPushback(res, newState, sizeof(vector));
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (int i = 0; i < choices->size; i++) {
|
||||
int *choice = malloc(sizeof(int));
|
||||
*choice = *((int *)(choices->data[i]));
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
bool select = *((bool *)(selected->data[i]));
|
||||
if (!select) {
|
||||
// 尝试:做出选择,更新状态
|
||||
*((bool *)selected->data[i]) = true;
|
||||
vectorPushback(state, choice, sizeof(int));
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
*((bool *)selected->data[i]) = false;
|
||||
vectorPopback(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
/* 全排列 I */
|
||||
vector *permutationsI(vector *nums) {
|
||||
vector *iState = newVector();
|
||||
|
||||
int select[3] = {false, false, false};
|
||||
vector *bSelected = newVector();
|
||||
for (int i = 0; i < nums->size; i++) {
|
||||
vectorPushback(bSelected, &select[i], sizeof(int));
|
||||
}
|
||||
|
||||
vector *res = newVector();
|
||||
|
||||
// 前序遍历
|
||||
backtrack(iState, nums, bSelected, res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -186,81 +508,381 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="permutations_ii.py"
|
||||
[class]{}-[func]{backtrack}
|
||||
def backtrack(
|
||||
state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]
|
||||
):
|
||||
"""回溯算法:全排列 II"""
|
||||
# 当状态长度等于元素数量时,记录解
|
||||
if len(state) == len(choices):
|
||||
res.append(list(state))
|
||||
return
|
||||
# 遍历所有选择
|
||||
duplicated = set[int]()
|
||||
for i, choice in enumerate(choices):
|
||||
# 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if not selected[i] and choice not in duplicated:
|
||||
# 尝试:做出选择,更新状态
|
||||
duplicated.add(choice) # 记录选择过的元素值
|
||||
selected[i] = True
|
||||
state.append(choice)
|
||||
# 进行下一轮选择
|
||||
backtrack(state, choices, selected, res)
|
||||
# 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = False
|
||||
state.pop()
|
||||
|
||||
[class]{}-[func]{permutations_ii}
|
||||
def permutations_ii(nums: list[int]) -> list[list[int]]:
|
||||
"""全排列 II"""
|
||||
res = []
|
||||
backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)
|
||||
return res
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="permutations_ii.cpp"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 II */
|
||||
void backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.size() == choices.size()) {
|
||||
res.push_back(state);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
unordered_set<int> duplicated;
|
||||
for (int i = 0; i < choices.size(); i++) {
|
||||
int choice = choices[i];
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i] && duplicated.find(choice) == duplicated.end()) {
|
||||
// 尝试:做出选择,更新状态
|
||||
duplicated.emplace(choice); // 记录选择过的元素值
|
||||
selected[i] = true;
|
||||
state.push_back(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.pop_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsII}
|
||||
/* 全排列 II */
|
||||
vector<vector<int>> permutationsII(vector<int> nums) {
|
||||
vector<int> state;
|
||||
vector<bool> selected(nums.size(), false);
|
||||
vector<vector<int>> res;
|
||||
backtrack(state, nums, selected, res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="permutations_ii.java"
|
||||
[class]{permutations_ii}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 II */
|
||||
void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.size() == choices.length) {
|
||||
res.add(new ArrayList<Integer>(state));
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
Set<Integer> duplicated = new HashSet<Integer>();
|
||||
for (int i = 0; i < choices.length; i++) {
|
||||
int choice = choices[i];
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i] && !duplicated.contains(choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
duplicated.add(choice); // 记录选择过的元素值
|
||||
selected[i] = true;
|
||||
state.add(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{permutations_ii}-[func]{permutationsII}
|
||||
/* 全排列 II */
|
||||
List<List<Integer>> permutationsII(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<List<Integer>>();
|
||||
backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="permutations_ii.cs"
|
||||
[class]{permutations_ii}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 II */
|
||||
void backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.Count == choices.Length) {
|
||||
res.Add(new List<int>(state));
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
ISet<int> duplicated = new HashSet<int>();
|
||||
for (int i = 0; i < choices.Length; i++) {
|
||||
int choice = choices[i];
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i] && !duplicated.Contains(choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
duplicated.Add(choice); // 记录选择过的元素值
|
||||
selected[i] = true;
|
||||
state.Add(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.RemoveAt(state.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{permutations_ii}-[func]{permutationsII}
|
||||
/* 全排列 II */
|
||||
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;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="permutations_ii.go"
|
||||
[class]{}-[func]{backtrackII}
|
||||
/* 回溯算法:全排列 II */
|
||||
func backtrackII(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if len(*state) == len(*choices) {
|
||||
newState := append([]int{}, *state...)
|
||||
*res = append(*res, newState)
|
||||
}
|
||||
// 遍历所有选择
|
||||
duplicated := make(map[int]struct{}, 0)
|
||||
for i := 0; i < len(*choices); i++ {
|
||||
choice := (*choices)[i]
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if _, ok := duplicated[choice]; !ok && !(*selected)[i] {
|
||||
// 尝试:做出选择,更新状态
|
||||
// 记录选择过的元素值
|
||||
duplicated[choice] = struct{}{}
|
||||
(*selected)[i] = true
|
||||
*state = append(*state, choice)
|
||||
// 进行下一轮选择
|
||||
backtrackI(state, choices, selected, res)
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
(*selected)[i] = false
|
||||
*state = (*state)[:len(*state)-1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsII}
|
||||
/* 全排列 II */
|
||||
func permutationsII(nums []int) [][]int {
|
||||
res := make([][]int, 0)
|
||||
state := make([]int, 0)
|
||||
selected := make([]bool, len(nums))
|
||||
backtrackII(&state, &nums, &selected, &res)
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="permutations_ii.swift"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 II */
|
||||
func backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if state.count == choices.count {
|
||||
res.append(state)
|
||||
return
|
||||
}
|
||||
// 遍历所有选择
|
||||
var duplicated: Set<Int> = []
|
||||
for (i, choice) in choices.enumerated() {
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if !selected[i], !duplicated.contains(choice) {
|
||||
// 尝试:做出选择,更新状态
|
||||
duplicated.insert(choice) // 记录选择过的元素值
|
||||
selected[i] = true
|
||||
state.append(choice)
|
||||
// 进行下一轮选择
|
||||
backtrack(state: &state, choices: choices, selected: &selected, res: &res)
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false
|
||||
state.removeLast()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsII}
|
||||
/* 全排列 II */
|
||||
func permutationsII(nums: [Int]) -> [[Int]] {
|
||||
var state: [Int] = []
|
||||
var selected = Array(repeating: false, count: nums.count)
|
||||
var res: [[Int]] = []
|
||||
backtrack(state: &state, choices: nums, selected: &selected, res: &res)
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="permutations_ii.js"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 II */
|
||||
function backtrack(state, choices, selected, res) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.length === choices.length) {
|
||||
res.push([...state]);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
const duplicated = new Set();
|
||||
choices.forEach((choice, i) => {
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i] && !duplicated.has(choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
duplicated.add(choice); // 记录选择过的元素值
|
||||
selected[i] = true;
|
||||
state.push(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.pop();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsII}
|
||||
/* 全排列 II */
|
||||
function permutationsII(nums) {
|
||||
const res = [];
|
||||
backtrack([], nums, Array(nums.length).fill(false), res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="permutations_ii.ts"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 II */
|
||||
function backtrack(
|
||||
state: number[],
|
||||
choices: number[],
|
||||
selected: boolean[],
|
||||
res: number[][]
|
||||
): void {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.length === choices.length) {
|
||||
res.push([...state]);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
const duplicated = new Set();
|
||||
choices.forEach((choice, i) => {
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i] && !duplicated.has(choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
duplicated.add(choice); // 记录选择过的元素值
|
||||
selected[i] = true;
|
||||
state.push(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.pop();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsII}
|
||||
/* 全排列 II */
|
||||
function permutationsII(nums: number[]): number[][] {
|
||||
const res: number[][] = [];
|
||||
backtrack([], nums, Array(nums.length).fill(false), res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="permutations_ii.dart"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 II */
|
||||
void backtrack(
|
||||
List<int> state,
|
||||
List<int> choices,
|
||||
List<bool> selected,
|
||||
List<List<int>> res,
|
||||
) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.length == choices.length) {
|
||||
res.add(List.from(state));
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
Set<int> duplicated = {};
|
||||
for (int i = 0; i < choices.length; i++) {
|
||||
int choice = choices[i];
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i] && !duplicated.contains(choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
duplicated.add(choice); // 记录选择过的元素值
|
||||
selected[i] = true;
|
||||
state.add(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.removeLast();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsII}
|
||||
/* 全排列 II */
|
||||
List<List<int>> permutationsII(List<int> nums) {
|
||||
List<List<int>> res = [];
|
||||
backtrack([], nums, List.filled(nums.length, false), res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="permutations_ii.rs"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 II */
|
||||
fn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if state.len() == choices.len() {
|
||||
res.push(state);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
let mut duplicated = HashSet::<i32>::new();
|
||||
for i in 0..choices.len() {
|
||||
let choice = choices[i];
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if !selected[i] && !duplicated.contains(&choice) {
|
||||
// 尝试:做出选择,更新状态
|
||||
duplicated.insert(choice); // 记录选择过的元素值
|
||||
selected[i] = true;
|
||||
state.push(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state.clone(), choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.remove(state.len() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutations_ii}
|
||||
/* 全排列 II */
|
||||
fn permutations_ii(nums: &mut [i32]) -> Vec<Vec<i32>> {
|
||||
let mut res = Vec::new();
|
||||
backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);
|
||||
res
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
Reference in New Issue
Block a user