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

90. Subsets II

时间:2018-10-28 17:58:32      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:mission   [1]   set   line   end   void   temp   ica   cto   

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: [1,2,2]
Output:
[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

AC code:

class Solution {
private:
    void helper(int begin, int len, vector<int> temp, vector<vector<int>>& res, vector<int> nums) {
        res.push_back(temp);
        for (int i = begin; i < len; ++i) {
            if (i != begin && nums[i] == nums[i-1]) continue;
            temp.push_back(nums[i]);
            helper(i+1, len, temp, res, nums);
            temp.pop_back();
        }
    }
    
public:
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        vector<int> temp;
        vector<vector<int>> res;
        sort(nums.begin(), nums.end());
        int len = nums.size();
        helper(0, len, temp, res, nums);
        return res;
    }
};
Runtime: 4 ms, faster than 100.00% of C++ online submissions for Subsets II.

 

 

90. Subsets II

标签:mission   [1]   set   line   end   void   temp   ica   cto   

原文地址:https://www.cnblogs.com/ruruozhenhao/p/9866112.html

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