码迷,mamicode.com
首页 > 其他好文 > 详细

Subsets II

时间:2016-06-07 16:18:58      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

Given a collection of integers that might contain duplicates, nums, return all possible subsets.

Note: 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],
  []
]
 1 class Solution {
 2 public:
 3     vector<vector<int>> subsetsWithDup(vector<int>& nums) {
 4                vector<vector<int>> res;
 5         res.push_back(vector<int> (0,0));
 6         
 7         vector<int> tmp;
 8         sort(nums.begin(),nums.end());
 9         sub(nums,0,tmp,res);
10         return res;
11         
12     }
13     void sub(vector<int> &nums,int s,vector<int> &tmp,vector<vector<int>> &res)
14     {
15         int len=nums.size();
16         for(int i=s;i<len;i++)
17         {
18             if(i>s&&nums[i]==nums[i-1])
19             continue;
20             tmp.push_back(nums[i]);
21             res.push_back(tmp);
22             sub(nums,i+1,tmp,res);
23             tmp.pop_back();
24         }
25     }
26         
27     
28 };

 

Subsets II

标签:

原文地址:http://www.cnblogs.com/hexhxy/p/5567260.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!