19.2.15 [LeetCode 78] Subsets
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 };
好像不太对啊……今天所有的memory usage好像都beat了100%……
注定失败的战争,也要拼尽全力去打赢它;
就算输,也要输得足够漂亮。