Update the code of permutations i and ii
This commit is contained in:
parent
918380b56a
commit
3f430fb85e
@ -3,6 +3,7 @@
|
|||||||
* Created Time: 2023-01-09
|
* Created Time: 2023-01-09
|
||||||
* Author: Reanon (793584285@qq.com)
|
* Author: Reanon (793584285@qq.com)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef LIST_NODE_H
|
#ifndef LIST_NODE_H
|
||||||
#define LIST_NODE_H
|
#define LIST_NODE_H
|
||||||
|
|
||||||
|
@ -18,26 +18,31 @@ void backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &sel
|
|||||||
int choice = choices[i];
|
int choice = choices[i];
|
||||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||||
if (!selected[i]) {
|
if (!selected[i]) {
|
||||||
// 尝试
|
// 尝试:做出选择,更新状态
|
||||||
selected[i] = true; // 做出选择
|
selected[i] = true;
|
||||||
state.push_back(choice); // 更新状态
|
state.push_back(choice);
|
||||||
backtrack(state, choices, selected, res);
|
backtrack(state, choices, selected, res);
|
||||||
// 回退
|
// 回退:撤销选择,恢复到之前的状态
|
||||||
selected[i] = false; // 撤销选择
|
selected[i] = false;
|
||||||
state.pop_back(); // 恢复到之前的状态
|
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 */
|
/* Driver Code */
|
||||||
int main() {
|
int main() {
|
||||||
vector<int> nums = {1, 2, 3};
|
vector<int> nums = {1, 2, 3};
|
||||||
|
|
||||||
// 回溯算法
|
vector<vector<int>> res = permutationsI(nums);
|
||||||
vector<int> state;
|
|
||||||
vector<bool> selected(nums.size(), false);
|
|
||||||
vector<vector<int>> res;
|
|
||||||
backtrack(state, nums, selected, res);
|
|
||||||
|
|
||||||
cout << "输入数组 nums = ";
|
cout << "输入数组 nums = ";
|
||||||
printVector(nums);
|
printVector(nums);
|
||||||
|
@ -19,27 +19,32 @@ void backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &sel
|
|||||||
int choice = choices[i];
|
int choice = choices[i];
|
||||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||||
if (!selected[i] && duplicated.find(choice) == duplicated.end()) {
|
if (!selected[i] && duplicated.find(choice) == duplicated.end()) {
|
||||||
// 尝试
|
// 尝试:做出选择,更新状态
|
||||||
duplicated.emplace(choice); // 记录选择过的元素值
|
duplicated.emplace(choice); // 记录选择过的元素值
|
||||||
selected[i] = true; // 做出选择
|
selected[i] = true;
|
||||||
state.push_back(choice); // 更新状态
|
state.push_back(choice);
|
||||||
backtrack(state, choices, selected, res);
|
backtrack(state, choices, selected, res);
|
||||||
// 回退
|
// 回退:撤销选择,恢复到之前的状态
|
||||||
selected[i] = false; // 撤销选择
|
selected[i] = false;
|
||||||
state.pop_back(); // 恢复到之前的状态
|
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 */
|
/* Driver Code */
|
||||||
int main() {
|
int main() {
|
||||||
vector<int> nums = {1, 1, 2};
|
vector<int> nums = {1, 1, 2};
|
||||||
|
|
||||||
// 回溯算法
|
vector<vector<int>> res = permutationsII(nums);
|
||||||
vector<int> state;
|
|
||||||
vector<bool> selected(nums.size(), false);
|
|
||||||
vector<vector<int>> res;
|
|
||||||
backtrack(state, nums, selected, res);
|
|
||||||
|
|
||||||
cout << "输入数组 nums = ";
|
cout << "输入数组 nums = ";
|
||||||
printVector(nums);
|
printVector(nums);
|
||||||
|
@ -21,24 +21,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.remove(state.size() - 1); // 恢复到之前的状态
|
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) {
|
public static void main(String[] args) {
|
||||||
int[] nums = { 1, 2, 3 };
|
int[] nums = { 1, 2, 3 };
|
||||||
List<List<Integer>> res = new ArrayList<List<Integer>>();
|
|
||||||
|
|
||||||
// 回溯算法
|
List<List<Integer>> res = permutationsI(nums);
|
||||||
backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);
|
|
||||||
|
|
||||||
System.out.println("输入数组 nums = " + Arrays.toString(nums));
|
System.out.println("输入数组 nums = " + Arrays.toString(nums));
|
||||||
System.out.println("所有排列 res = " + res);
|
System.out.println("所有排列 res = " + res);
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ import java.util.*;
|
|||||||
|
|
||||||
public class permutations_ii {
|
public class permutations_ii {
|
||||||
/* 回溯算法:全排列 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) {
|
if (state.size() == choices.length) {
|
||||||
res.add(new ArrayList<Integer>(state));
|
res.add(new ArrayList<Integer>(state));
|
||||||
@ -22,24 +22,29 @@ 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.remove(state.size() - 1); // 恢复到之前的状态
|
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) {
|
public static void main(String[] args) {
|
||||||
int[] nums = { 1, 2, 2 };
|
int[] nums = { 1, 2, 2 };
|
||||||
List<List<Integer>> res = new ArrayList<List<Integer>>();
|
|
||||||
|
|
||||||
// 回溯算法
|
List<List<Integer>> res = permutationsII(nums);
|
||||||
backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);
|
|
||||||
|
|
||||||
System.out.println("输入数组 nums = " + Arrays.toString(nums));
|
System.out.println("输入数组 nums = " + Arrays.toString(nums));
|
||||||
System.out.println("所有排列 res = " + res);
|
System.out.println("所有排列 res = " + res);
|
||||||
|
@ -22,22 +22,27 @@ def backtrack(
|
|||||||
for i, choice in enumerate(choices):
|
for i, choice in enumerate(choices):
|
||||||
# 剪枝:不允许重复选择元素
|
# 剪枝:不允许重复选择元素
|
||||||
if not selected[i]:
|
if not selected[i]:
|
||||||
# 尝试
|
# 尝试:做出选择,更新状态
|
||||||
selected[i] = True # 做出选择
|
selected[i] = True
|
||||||
state.append(choice) # 更新状态
|
state.append(choice)
|
||||||
backtrack(state, choices, selected, res)
|
backtrack(state, choices, selected, res)
|
||||||
# 回退
|
# 回退:撤销选择,恢复到之前的状态
|
||||||
selected[i] = False # 撤销选择
|
selected[i] = False
|
||||||
state.pop() # 恢复到之前的状态
|
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"""
|
"""Driver Code"""
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
nums = [1, 2, 3]
|
nums = [1, 2, 3]
|
||||||
res = []
|
|
||||||
|
|
||||||
# 回溯算法
|
res = permutations_i(nums)
|
||||||
backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)
|
|
||||||
|
|
||||||
print(f"输入数组 nums = {nums}")
|
print(f"输入数组 nums = {nums}")
|
||||||
print(f"所有排列 res = {res}")
|
print(f"所有排列 res = {res}")
|
||||||
|
@ -23,23 +23,28 @@ def backtrack(
|
|||||||
for i, choice in enumerate(choices):
|
for i, choice in enumerate(choices):
|
||||||
# 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
# 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||||
if not selected[i] and choice not in duplicated:
|
if not selected[i] and choice not in duplicated:
|
||||||
# 尝试
|
# 尝试:做出选择,更新状态
|
||||||
duplicated.add(choice) # 记录选择过的元素值
|
duplicated.add(choice) # 记录选择过的元素值
|
||||||
selected[i] = True # 做出选择
|
selected[i] = True
|
||||||
state.append(choice) # 更新状态
|
state.append(choice)
|
||||||
backtrack(state, choices, selected, res)
|
backtrack(state, choices, selected, res)
|
||||||
# 回退
|
# 回退:撤销选择,恢复到之前的状态
|
||||||
selected[i] = False # 撤销选择
|
selected[i] = False
|
||||||
state.pop() # 恢复到之前的状态
|
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"""
|
"""Driver Code"""
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
nums = [1, 2, 2]
|
nums = [1, 2, 2]
|
||||||
res = []
|
|
||||||
|
|
||||||
# 回溯算法
|
res = permutations_ii(nums)
|
||||||
backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)
|
|
||||||
|
|
||||||
print(f"输入数组 nums = {nums}")
|
print(f"输入数组 nums = {nums}")
|
||||||
print(f"所有排列 res = {res}")
|
print(f"所有排列 res = {res}")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user