子集
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
说明:解集不能包含重复的子集。
示例:
输入: nums = [1,2,3]
输出:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
class Solution { public: vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int>> result; result.resize(1); /* if(nums.size() == 1) { vector<int> temp; result.push_back(temp); vector<int> temp1; temp1.push_back(nums[0]); result.push_back(temp1); return result; } vector<vector<int>> pre_result; vector<int> nums2; nums2.insert(nums2.begin(), nums.begin()+1, nums.end()); pre_result = subsets(nums2); for(int i = 0; i < pre_result.size(); i++){ vector<int> temp = pre_result[i]; temp.push_back(nums[0]); result.push_back(temp); result.push_back(pre_result[i]); } return result; */ for(int i = 0; i < nums.size();i ++){ int len = result.size(); for(int j = 0; j < len; j++){ vector<int> temp = result[j]; temp.push_back(nums[i]); result.push_back(temp); } } return result; } };
The Safest Way to Get what you Want is to Try and Deserve What you Want.