栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

47. 全排列 II

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

47. 全排列 II

把所有的数全部统计出来,根据index,每次从其中拿出一个,++index。自然用的都是递归。不要再像全排列I似的swap。官方题解写的很清楚了。为了避免重复,每次在for中添加数字的时候不让添加重复的数字,因此传入的是哈希表。

#include
#include
#include
#include
using namespace std;
vector >ret;
int n;
void recursion(vectora, unordered_mapnotused, int index)
{
	if(index >= n)
		ret.push_back(a);
	for(auto notindex : notused)
	{
		auto newnotused = notused;
		if(1==notindex.second)
			newnotused.erase(notindex.first);
		else
			--newnotused.find(notindex.first)->second;

		auto b = a;
		b.push_back(notindex.first);
		recursion(b, newnotused, index+1);
	}
	return;
}

vector> permuteUnique(vector& nums) {
	n = (int)nums.size();
	unordered_mapnotused;
	for(int i=0; i<(int)nums.size(); ++i)
	{
		auto it_notused = notused.find(nums[i]);
		if(it_notused == notused.end())
			notused.insert(pair(nums[i], 1));
		else
			++it_notused->second;
	}
	vectora;
	recursion(a, notused, 0);
	return ret;
}

int main(void)
{
	vectora={2,2,1,1};
	permuteUnique(a);
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/656994.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号