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

Permutations II

2019-11-14 11:36:21
字体:
来源:转载
供稿:网友

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,[1,1,2] have the following unique permutations:[1,1,2][1,2,1], and [2,1,1].

void visit(vector<int> &num, int n, int pos){	if (n == pos)	{		for (int i = 0; i < num.size(); i++)		{			cout << num[i] << " ";		}		cout << endl;		return;	}	for (int i = pos; i < n; i++)	{		if (num[i] == num[pos] && i != pos)		{			continue;		}		swap(num[i], num[pos]);		visit(num, n, pos+1);		swap(num[i], num[pos]);	}}void fun(vector<int> &num){	int n = num.size();	sort(num.begin(), num.end());	visit(num, n, 0);}


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