Subsets

来源:互联网 发布:美瞳线 知乎 编辑:程序博客网 时间:2024/05/02 08:20

先对S进行排序, 之后深搜, 每个数字有选或不选两种情况。

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


原创粉丝点击