Subsets II

来源:互联网 发布:百度云搜索cms程序 编辑:程序博客网 时间:2024/05/29 03:29

Given a collection of integers that might contain duplicates, S, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

 

For example,
If S = [1,2,2], a solution is:

[  [2],  [1],  [1,2,2],  [2,2],  [1,2],  []]

void visit(vector<int> &S, int n, int pos, vector<int> &buf, vector<vector<int> > &result){if (pos == n){result.push_back(buf);return;}if ((pos < 1) || (S[pos] != S[pos-1]) || (S[pos] == S[pos-1] && buf.size() > 0 && S[pos] == buf[buf.size()-1])){buf.push_back(S[pos]);visit(S, n, pos+1, buf, result);buf.pop_back();}visit(S, n, pos+1, buf, result);}vector<vector<int> > subsets(vector<int> &S){vector<vector<int> > result;int n = S.size();vector<int> buf;visit(S, n, 0, buf, result);return result;}


0 0
原创粉丝点击