集合{1,2,3}有Leetcode 78 子集 - 图1个子集,本题的思路就是用二进制来模拟,1表示在本子集中包含,否则就不包含;
    时间复杂度:Leetcode 78 子集 - 图2

    1. class Solution {
    2. public:
    3. vector<vector<int>> subsets(vector<int>& nums) {
    4. int n = nums.size();
    5. vector<vector<int>> ans;
    6. for(int i=0;i<1<<n;i++){
    7. vector<int> now;
    8. for(int j=0;j<n;j++){
    9. if(i>>j&1) now.push_back(nums[j]);
    10. }
    11. ans.push_back(now);
    12. }
    13. return ans;
    14. }
    15. };