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

78. Subsets

时间:2018-10-24 16:06:20      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:ssi   BMI   void   put   solution   must   []   ica   mis   

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

AC code:

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int>> res;
        vector<int> temp;
        int len = nums.size();
        solve_subset(0, len, res, temp, nums);
        return res;
    }
    void solve_subset(int begin, int n, vector<vector<int>>& res, vector<int>& temp, vector<int>& nums) {
        res.push_back(temp);
        for (int i = begin; i < n; ++i) {
            temp.push_back(nums[i]);
            solve_subset(i+1, n, res, temp, nums);
            temp.pop_back();
        }
    }
};

Runtime: 4 ms, faster than 100.00% of C++ online submissions for Subsets.

 

78. Subsets

标签:ssi   BMI   void   put   solution   must   []   ica   mis   

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

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