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

leetcode90 Subsets II

时间:2016-01-06 17:36:56      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

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],
  []
]
技术分享
 1 class Solution {
 2 public:
 3     vector<vector<int>> subsetsWithDup(vector<int>& nums) {
 4         sort(nums.begin(),nums.end());
 5         vector<vector<int>> ans;
 6         vector<int> cur;
 7         ans.push_back(cur);
 8         dep(ans,nums,0,cur);
 9         return ans;
10     }
11     void dep(vector<vector<int>>&ans,vector<int>&nums,int pos,vector<int>&cur)
12     {
13         int size=nums.size();
14         if(pos>=size)
15             return;
16         for(int i=pos;i<size;i++)
17         {
18             if(i!=pos&&nums[i]==nums[i-1])
19                 continue;
20             
21             cur.push_back(nums[i]);
22             ans.push_back(cur);//解决自某个元素后都不选则添加不进结果集的情况
23             dep(ans,nums,i+1,cur);
24             cur.erase(cur.end()-1);
25         }
26     }
27 };
View Code

 

leetcode90 Subsets II

标签:

原文地址:http://www.cnblogs.com/jsir2016bky/p/5105998.html

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