详细思路
sort,每次都要存放答案,向右找,枚举,放进去,或者不要,if(!vis[i-1]&&i i-1)防止同一个数,begin向右找防止只是交换 精确定义 depth需要判断 i需要判断 begin起点class Solution { public: vector<vector<int>> subsetsWithDup(vector<int>& nums) { int n=nums.size(); sort(nums.begin(),nums.end()); vector<vector<int>>ans; vector<int>ans1; vector<int>vis(n,0); dfs(0,0,ans,ans1,nums,vis); return ans; } void dfs(int depth,int begin,vector<vector<int>>&ans,vector<int>&ans1,vector<int>&nums,vector<int>&vis){ ans.push_back(ans1); if(depth==nums.size())return; for(int i=begin;i<nums.size();i++){ if(i-1>=0&&!vis[i-1]&&nums[i]==nums[i-1])continue; ans1.push_back(nums[i]); vis[i]=1; dfs(depth+1,i+1,ans,ans1,nums,vis); vis[i]=0; ans1.pop_back(); } } };