[LEETCODE] Subsets

LINK:http://leetcode.com/onlinejudge#question_78

题意:给出一个数组, 求其子集。

class Solution {
public:
    vector<vector<int> > ans;
    void dfs(vector<int> &S, int now, vector<int> t){
        if(now >= S.size()){
            sort(t.begin(),t.end());
            ans.push_back(t);
            return;
        }
        dfs(S, now + 1, t);
        t.push_back(S[now]);
        dfs(S, now + 1, t);
    }
    vector<vector<int> > subsets(vector<int> &S) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        ans.clear();
        vector<int> t;
        dfs(S, 0, t);
        return ans;
    }
};

  每次总感觉自己的DFS写的很怂的样子.

posted @ 2013-04-23 21:22  Shamaoer  阅读(123)  评论(0)    收藏  举报