本题给定的集合中含有重复元素的所以这里需要涉及去重的问题集合中的元素要先排序使用一个集合来标记哪些元素被使用过 #include <iostream> #include <vector>class Solution { private:std::vector<int> path;std::vector<std::vector<int>> result;void backtracking(std::vector<int>& nums, int startIndex, std::vector<bool>& used) {result.push_back(path);for (int i = startIndex; i < nums.size(); ++i) {if (i > 0 && nums.at(i) == nums.at(i - 1) && used.at(i - 1) == false)continue;path.push_back(nums.at(i));used.at(i) = true;backtracking(nums, i + 1, used);used.at(i) = false;path.pop_back();}} public:std::vector<std::vector<int>> subsetsWithDup(std::vector<int>& nums) {std::vector<bool> used(nums.size(), false);std::sort(nums.begin(), nums.end());backtracking(nums, 0, used);return result;} };int main() {Solution s;return 0; } 汇总