Files
leetcode-master/problems/0078.子集.md
youngyangyang04 3ee636ec90 Update
2020-08-21 10:31:54 +08:00

50 lines
891 B
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 题目地址
# 第78题. 子集
给定一组不含重复元素的整数数组 nums返回该数组所有可能的子集幂集
说明:解集不能包含重复的子集。
示例:
输入: nums = [1,2,3]
输出:
[
[3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
# 思路
# C++代码
```
class Solution {
private:
void backtracking(vector<int>& nums, vector<vector<int>>& result, vector<int>& vec, int startIndex) {
result.push_back(vec);
for (int i = startIndex; i < nums.size(); i++) {
vec.push_back(nums[i]);
backtracking(nums, result, vec, i + 1);
vec.pop_back();
}
}
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> result;
vector<int> vec;
backtracking(nums, result, vec, 0);
return result;
}
};
```