90. Subsets II
Posted いいえ敗者
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了90. Subsets II相关的知识,希望对你有一定的参考价值。
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,2]
, a solution is:
[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
class Solution { public: void dfs(vector<vector<int>>&u,int x,vector<int>& nums,vector<int>&v,int cnt,int n){ if(cnt==n){ //n++; u.push_back(v); return ; } for(int i=x;i<nums.size();i++){ if(nums[i]==nums[i-1]&&i>x)continue; v.push_back(nums[i]); dfs(u,i+1,nums,v,cnt+1,n); v.pop_back(); } } vector<vector<int>> subsetsWithDup(vector<int>& nums) { vector<vector<int>>u; vector<int>v; sort(nums.begin(),nums.end()); for(int i=0;i<=nums.size();i++){ dfs(u,0,nums,v,0,i); } return u; } };
以上是关于90. Subsets II的主要内容,如果未能解决你的问题,请参考以下文章