标签:
Given a set of distinct integers, nums, return all possible subsets.
Note:
For example,
If nums = [1,2,3]
,
a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
class Solution { public: void int_to_bool(int n,vector<int>&index) { int len=index.size(); index.clear(); index.resize(len); int i=0; while(n) { int temp=n%2; index[i]=temp; i++; n/=2; } } vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int>> res; if(nums.size()==0) return res; sort(nums.begin(),nums.end()); int len=nums.size(); int max=(1<<nums.size()); vector<int> index(len,0); for(int i=0;i<max;i++) { int_to_bool(i,index); vector<int> temp; for(int j=0;j<len;j++) { if(index[j]==1) temp.push_back(nums[j]); } res.push_back(temp); } return res; } };
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/sinat_24520925/article/details/46835209