Update the code of permutations i and ii

This commit is contained in:
krahets 2023-04-27 01:18:18 +08:00
parent 918380b56a
commit 3f430fb85e
7 changed files with 91 additions and 60 deletions

View file

@ -3,6 +3,7 @@
* Created Time: 2023-01-09
* Author: Reanon (793584285@qq.com)
*/
#ifndef LIST_NODE_H
#define LIST_NODE_H

View file

@ -18,26 +18,31 @@ void backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &sel
int choice = choices[i];
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
if (!selected[i]) {
// 尝试
selected[i] = true; // 做出选择
state.push_back(choice); // 更新状态
// 尝试:做出选择,更新状态
selected[i] = true;
state.push_back(choice);
backtrack(state, choices, selected, res);
// 回退
selected[i] = false; // 撤销选择
state.pop_back(); // 恢复到之前的状态
// 回退:撤销选择,恢复到之前的状态
selected[i] = false;
state.pop_back();
}
}
}
/* 全排列 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;
}
/* Driver Code */
int main() {
vector<int> nums = {1, 2, 3};
// 回溯算法
vector<int> state;
vector<bool> selected(nums.size(), false);
vector<vector<int>> res;
backtrack(state, nums, selected, res);
vector<vector<int>> res = permutationsI(nums);
cout << "输入数组 nums = ";
printVector(nums);

View file

@ -19,27 +19,32 @@ void backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &sel
int choice = choices[i];
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
if (!selected[i] && duplicated.find(choice) == duplicated.end()) {
// 尝试
// 尝试:做出选择,更新状态
duplicated.emplace(choice); // 记录选择过的元素值
selected[i] = true; // 做出选择
state.push_back(choice); // 更新状态
selected[i] = true;
state.push_back(choice);
backtrack(state, choices, selected, res);
// 回退
selected[i] = false; // 撤销选择
state.pop_back(); // 恢复到之前的状态
// 回退:撤销选择,恢复到之前的状态
selected[i] = false;
state.pop_back();
}
}
}
/* 全排列 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;
}
/* Driver Code */
int main() {
vector<int> nums = {1, 1, 2};
// 回溯算法
vector<int> state;
vector<bool> selected(nums.size(), false);
vector<vector<int>> res;
backtrack(state, nums, selected, res);
vector<vector<int>> res = permutationsII(nums);
cout << "输入数组 nums = ";
printVector(nums);

View file

@ -21,23 +21,28 @@ 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.remove(state.size() - 1); // 恢复到之前的状态
// 回退撤销选择恢复到之前的状态
selected[i] = false;
state.remove(state.size() - 1);
}
}
}
/* 全排列 I */
static 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;
}
public static void main(String[] args) {
int[] nums = { 1, 2, 3 };
List<List<Integer>> res = new ArrayList<List<Integer>>();
// 回溯算法
backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);
List<List<Integer>> res = permutationsI(nums);
System.out.println("输入数组 nums = " + Arrays.toString(nums));
System.out.println("所有排列 res = " + res);

View file

@ -10,7 +10,7 @@ import java.util.*;
public class permutations_ii {
/* 回溯算法:全排列 II */
public static void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
static void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
// 当状态长度等于元素数量时记录解
if (state.size() == choices.length) {
res.add(new ArrayList<Integer>(state));
@ -22,24 +22,29 @@ 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.remove(state.size() - 1); // 恢复到之前的状态
// 回退撤销选择恢复到之前的状态
selected[i] = false;
state.remove(state.size() - 1);
}
}
}
/* 全排列 II */
static 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;
}
public static void main(String[] args) {
int[] nums = { 1, 2, 2 };
List<List<Integer>> res = new ArrayList<List<Integer>>();
// 回溯算法
backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);
List<List<Integer>> res = permutationsII(nums);
System.out.println("输入数组 nums = " + Arrays.toString(nums));
System.out.println("所有排列 res = " + res);

View file

@ -22,22 +22,27 @@ def backtrack(
for i, choice in enumerate(choices):
# 剪枝:不允许重复选择元素
if not selected[i]:
# 尝试
selected[i] = True # 做出选择
state.append(choice) # 更新状态
# 尝试:做出选择,更新状态
selected[i] = True
state.append(choice)
backtrack(state, choices, selected, res)
# 回退
selected[i] = False # 撤销选择
state.pop() # 恢复到之前的状态
# 回退:撤销选择,恢复到之前的状态
selected[i] = False
state.pop()
def permutations_i(nums: list[int]) -> list[list[int]]:
"""全排列 I"""
res = []
backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)
return res
"""Driver Code"""
if __name__ == "__main__":
nums = [1, 2, 3]
res = []
# 回溯算法
backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)
res = permutations_i(nums)
print(f"输入数组 nums = {nums}")
print(f"所有排列 res = {res}")

View file

@ -23,23 +23,28 @@ def backtrack(
for i, choice in enumerate(choices):
# 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
if not selected[i] and choice not in duplicated:
# 尝试
# 尝试:做出选择,更新状态
duplicated.add(choice) # 记录选择过的元素值
selected[i] = True # 做出选择
state.append(choice) # 更新状态
selected[i] = True
state.append(choice)
backtrack(state, choices, selected, res)
# 回退
selected[i] = False # 撤销选择
state.pop() # 恢复到之前的状态
# 回退:撤销选择,恢复到之前的状态
selected[i] = False
state.pop()
def permutations_ii(nums: list[int]) -> list[list[int]]:
"""全排列 II"""
res = []
backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)
return res
"""Driver Code"""
if __name__ == "__main__":
nums = [1, 2, 2]
res = []
# 回溯算法
backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)
res = permutations_ii(nums)
print(f"输入数组 nums = {nums}")
print(f"所有排列 res = {res}")