题解 | #集合的所有子集(一)#
集合的所有子集(一)
https://www.nowcoder.com/practice/c333d551eb6243e0b4d92e37a06fbfc9
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param S int整型vector * @return int整型vector<vector<>> */ vector<int> makeset(int bitset, vector<int>& S) { int pos = 0; vector<int> res; while(bitset) { if (bitset & 1) { res.push_back(S[pos]); } pos++; bitset = bitset >> 1; } for (auto it : res) cout << it << ' '; cout << endl; return res; } vector<vector<int> > subsets(vector<int>& S) { vector<vector<int>> res; if (S.size() == 0) return res; int len = (1 << S.size()) - 1; for (int i = 0; i <= len; i++) { res.push_back(makeset(i, S)); } sort(res.begin(), res.end(), [](vector<int>& a, vector<int>& b){ return a.size() < b.size(); }); return res; } };