首页 > 其他 > 详细

19.2.15 [LeetCode 78] Subsets

时间:2019-02-15 16:44:05      阅读:138      评论:0      收藏:0      [点我收藏+]

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],
  []
]
技术分享图片
 1 class Solution {
 2 public:
 3     void build(vector<vector<int>>&ans, vector<int>&now, vector<int>&nums, int i) {
 4         for (; i < nums.size(); i++) {
 5             now.push_back(nums[i]);
 6             ans.push_back(now);
 7             build(ans, now, nums, i + 1);
 8             now.pop_back();
 9         }
10     }
11     vector<vector<int>> subsets(vector<int>& nums) {
12         vector<vector<int>>ans;
13         vector<int>now;
14         ans.push_back(vector<int>());
15         build(ans, now, nums, 0);
16         return ans;
17     }
18 };
View Code

好像不太对啊……今天所有的memory usage好像都beat了100%……

19.2.15 [LeetCode 78] Subsets

原文:https://www.cnblogs.com/yalphait/p/10384233.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!