class Solution {
public:
vector<vector<int> > subsets(vector<int> &s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(s.begin(), s.end());
vector<vector<int> > res;
if (s.size() == 0) return res;
int i = 0;
int total = 1 << s.size();
while (i < total)
{
int cur = i;
int idx = 0;
vector<int> temp;
while (cur)
{
if (cur & 1)
{
temp.push_back(s[idx]);
}
++idx;
cur >>= 1;
}
res.push_back(temp);
++i;
}
return res;
}
};[Leetcode] Subsets
最新推荐文章于 2019-04-17 12:01:23 发布
本文介绍了一个使用C++实现的子集生成算法。该算法通过位操作遍历所有可能的组合来生成给定整数集合的所有子集。文章提供的代码首先对输入向量进行排序,然后利用位操作来构造所有可能的子集。
633

被折叠的 条评论
为什么被折叠?



