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

LeetCode:Subsets

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

标签:

Subsets




Total Accepted: 99033 Total Submissions: 310195 Difficulty: Medium

Given a set of distinct integers, nums, return all possible subsets.

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

Subscribe to see which companies asked this question

Hide Similar Problems
 (M) Generalized Abbreviation



























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

标签:

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

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