力扣 题目78- 子集
题目
题解
将77题进行改造 由满足一定长度加入改为当加入字符时 就放入结果集
代码
1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 int loop(vector<int>& nums, vector<vector<int>> &result, vector<int>& alone,int begin) { 5 for (int i = begin; i < nums.size(); i++) { 6 alone.push_back(nums[i]); 7 result.push_back(alone); 8 loop(nums, result, alone, i + 1); 9 alone.pop_back(); 10 } 11 return 0; 12 } 13 class Solution { 14 public: 15 vector<vector<int>> subsets(vector<int>& nums) { 16 vector<vector<int>> result; 17 result.push_back({}); 18 vector<int> alone; 19 loop(nums, result, alone,0); 20 return result; 21 } 22 }; 23 24 int main() { 25 Solution sol; 26 vector<int> nums = { 1, 2, 3,4,5 }; 27 vector<vector<int>> result=sol.subsets(nums); 28 for (int i = 0; i < result.size(); i++) { 29 for (int j = 0; j < result[i].size(); j++) { 30 cout << result[i][j] << " "; 31 } 32 cout << endl; 33 } 34 }