这题我是改了之前的那个就ac了,2刷一定要刷,跟第一道一起刷,因为这个dfs有问题的,会出现重复,需要改!!!
class Solution {public: vector<vector<int>>fve; set<vector<int>>ve; set<vector<int>>ve1; vector<int>vee; void dfs(vector<int>& nums, int k, int now, int num){ vee.push_back(nums[now]); if(num + 1 == k){ ve.insert(vee); } else{ for(int i = now + 1; i < nums.size(); ++ i) dfs(nums, k, i, num + 1); } vee.pop_back(); if(now + 1 < nums.size()) dfs(nums, k, now + 1, num); } vector<vector<int>> subsetsWithDup(vector<int>& nums) { ve.insert(vee); for(int i = 1; i <= nums.size(); ++ i){ vee.clear(); dfs(nums, i, 0, 0); } for(set<vector<int>>::iterator it = ve.begin(); it != ve.end(); ++ it){ vee = *it; sort(vee.begin(), vee.end()); ve1.insert(vee); } for(set<vector<int>>::iterator it = ve1.begin(); it != ve1.end(); ++ it){ vee = *it; fve.push_back(vee); } return fve; }};新闻热点
疑难解答