题解 | #集合的所有子集(一)#
集合的所有子集(一)
https://www.nowcoder.com/practice/c333d551eb6243e0b4d92e37a06fbfc9
#include <vector>
class Solution {
private:
vector<int> temp;
vector<vector<int>> ans;
public:
void dfs(vector<int> &S, int i){
ans.push_back(temp);
//这里少了一个if进行判断,其实是因为for循环中潜在的进行了判断
for(int j = i; j < S.size(); j++){//一层for循环,相当于每个结点下面的子节点
temp.push_back(S[j]);
dfs(S, j + 1);
temp.pop_back();//弹出最后一个结点,方便回溯
}
return;
}
vector<vector<int>> subsets(vector<int>& S) {
dfs(S, 0);
return ans;
}
};
SHEIN希音公司福利 256人发布