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

LeetCode:Subsets II

时间:2016-06-12 02:20:20      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

Subsets II




Total Accepted: 69579 Total Submissions: 224799 Difficulty: Medium

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],
  []
]

Subscribe to see which companies asked this question

Hide Tags
 Array Backtracking

























c++ code:

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int>> combs;
        vector<int> comb;
        combine(combs, comb, nums, 0);
        return combs;
    }
    
    // 自定义函数
    void combine(vector<vector<int>> &combs, vector<int> &comb,vector<int>& nums,int begin) {
        
        combs.push_back(comb);
        
        for(int i=begin;i<nums.size();i++) {
            comb.push_back(nums[i]);
            combine(combs,comb,nums,i+1);
            comb.pop_back();
        }
    }
    
};


LeetCode:Subsets II

标签:

原文地址:http://blog.csdn.net/itismelzp/article/details/51627541

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