标签:des style blog color io os strong for div
Given a set of distinct integers, S, return all possible subsets.
Note:
For example,
If S = [1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
算法:该题的解空间类似一个排列树,但是又不完全相同,树种根到所有节点的路径是一个解,还有不要忘了空集。直接用深度优先遍历解决:
10/10test cases passed
Runtime::44ms
1 class Solution { 2 public: 3 vector<vector<int> > subsets(vector<int> &S) { 4 vector<vector<int>> result; 5 int len=S.size(); 6 if(S.size()<=0) return result; 7 sort(S.begin(),S.end()); 8 vector<int> temp; 9 doit(result,S,0,len,temp); 10 return result; 11 } 12 void doit(vector<vector<int>> &result,vector<int> &s,int dep,int len,vector<int> &temp) 13 { 14 result.push_back(temp); 15 for(int i=dep;i<len;i++) 16 { 17 temp.push_back(s[i]); 18 doit(result,s,i+1,len,temp); 19 temp.pop_back(); 20 } 21 } 22 };
标签:des style blog color io os strong for div
原文地址:http://www.cnblogs.com/sqxw/p/3976604.html