From a40cbdf78efc9b8ba186fdc0a90668d40b92468e Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Fri, 3 Feb 2023 17:01:25 +0800 Subject: [PATCH] s0047 --- include/s0047_permutations_ii.hpp | 14 ++++++ notes/src/permute.md | 80 +++++++++++++++++++++++++++++++ src/s0047_permutations_ii.cpp | 33 +++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 include/s0047_permutations_ii.hpp create mode 100644 src/s0047_permutations_ii.cpp diff --git a/include/s0047_permutations_ii.hpp b/include/s0047_permutations_ii.hpp new file mode 100644 index 0000000..e8b776e --- /dev/null +++ b/include/s0047_permutations_ii.hpp @@ -0,0 +1,14 @@ +#ifndef S0047_PERMUTATIONS_II_HPP +#define S0047_PERMUTATIONS_II_HPP + +#include +#include + +using namespace std; + +class S0047 { + public: + vector> permuteUnique(vector& nums); +}; + +#endif diff --git a/notes/src/permute.md b/notes/src/permute.md index 7ac49ba..c19b3b2 100644 --- a/notes/src/permute.md +++ b/notes/src/permute.md @@ -1,3 +1,83 @@ # 排列问题 ## [46. 全排列](https://leetcode.cn/problems/permutations/) + +## [47. 全排列 II](https://leetcode.cn/problems/permutations-ii/) + +和 s0046 相比就加了去重。有两种去重思路,一个是用哈希表记录每一层的使用情况。另一种是排序 + 判断,后者性能更好所以优先选择后者。 + +哈希表法: + +```cpp +#include "s0047_permutations_ii.hpp" + +void permuteUniqueDFS(vector &path, vector> &result, + vector &used, vector &nums) { + int len = nums.size(); + // 终止条件 + if (path.size() == len) { + result.push_back(path); + return; + } + + // 创建一个哈希表用来记录当前层中使用过的元素 + unordered_map map; + + // 开始迭代 + for (int i{0}; i < len; ++i) { + // 如果当前元素在树枝或树层使用过,则跳过 + if (used[i] || map.count(nums[i]) == 1) continue; + // 否则处理当前节点 + map[nums[i]] = true; + path.push_back(nums[i]); + used[i] = true; + permuteUniqueDFS(path, result, used, nums); + used[i] = false; + path.pop_back(); + } +} + +vector> S0047::permuteUnique(vector &nums) { + vector path{}; + vector> result{}; + vector used(nums.size(), false); + permuteUniqueDFS(path, result, used, nums); + return result; +} +``` + +排序 + 判断: + +```cpp +void permuteUniqueDFS(vector &path, vector> &result, + vector &used, vector &nums, int startIndex) { + int len = nums.size(); + // 终止条件 + if (path.size() == len) { + result.push_back(path); + return; + } + + // 开始迭代 + for (int i{0}; i < len; ++i) { + // 如果当前元素在树层使用过,则跳过 + if (used[i] || (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false)) + continue; + // 否则处理当前节点 + path.push_back(nums[i]); + used[i] = true; + permuteUniqueDFS(path, result, used, nums, i + 1); + used[i] = false; + path.pop_back(); + } +} + +vector> S0047::permuteUnique(vector &nums) { + vector path{}; + vector> result{}; + vector used(nums.size(), false); + sort(nums.begin(), nums.end()); + permuteUniqueDFS(path, result, used, nums, 0); + return result; +} +``` diff --git a/src/s0047_permutations_ii.cpp b/src/s0047_permutations_ii.cpp new file mode 100644 index 0000000..93e87c5 --- /dev/null +++ b/src/s0047_permutations_ii.cpp @@ -0,0 +1,33 @@ +#include "s0047_permutations_ii.hpp" + +void permuteUniqueDFS(vector &path, vector> &result, + vector &used, vector &nums, int startIndex) { + int len = nums.size(); + // 终止条件 + if (path.size() == len) { + result.push_back(path); + return; + } + + // 开始迭代 + for (int i{0}; i < len; ++i) { + // 如果当前元素在树层使用过,则跳过 + if (used[i] || (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false)) + continue; + // 否则处理当前节点 + path.push_back(nums[i]); + used[i] = true; + permuteUniqueDFS(path, result, used, nums, i + 1); + used[i] = false; + path.pop_back(); + } +} + +vector> S0047::permuteUnique(vector &nums) { + vector path{}; + vector> result{}; + vector used(nums.size(), false); + sort(nums.begin(), nums.end()); + permuteUniqueDFS(path, result, used, nums, 0); + return result; +}