首页 > 学院 > 开发设计 > 正文

Subsets II

2019-11-14 09:09:50
字体:
来源:转载
供稿:网友

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;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表