Given a set of distinct integers,S, Return all possible subsets.
Note:
Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets.
For example,
IfS=[1,2,3], A solution is:
[[3], [1], [2], [, 3], [], [], [], [], [] solutions: because the number in the S set is different, there must be 2 ^ n subsets. It is estimated that the n value in the background data should not be greater than 32, so we can use the binary value of an integer to indicate whether a number appears in the set and then enumerate it. solution code:class Solution {public: vector
> subsets(vector
&S) { int n = S.size(); sort(S.begin(),S.end()); vector
> res; for (int i = 0; i < 1 << n; ++i) { vector
vec ; int tmp = i , cnt = 0 ; while (tmp) { if (tmp & 1) vec.push_back(S[cnt]); tmp >>= 1 , ++cnt ; } res.push_back(vector
(vec.begin(),vec.end())); } return res; }};