leetcode 78. subset (c ++)

Given a set of no repeating element integer array nums, which returns an array of all possible subsets (power set).

Description: Solution Set can not contain duplicate subsets.

Example:

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

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int> > res(1);
        sort(nums.begin(), nums.end());
        for (int i = 0; i < nums.size(); ++i) {
            int size = res.size();
            for (int j = 0; j < size; ++j) {
                res.push_back(res[j]);
                res.back().push_back(nums[i]);
            }
        }
        return res;
    }
};

 

Guess you like

Origin www.cnblogs.com/xiaotongtt/p/11318169.html