leecode 解题总结:90. Subsets II

#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
/*
问题:
Given a collection of integers that might contain duplicates, nums, return all possible subsets.

Note: The solution set must not contain duplicate subsets.

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

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

分析:之前遇到的是没有重复数字的。
没有重复数字的{1,2}
A={},{1},{2},{1,2}
A中每个子集插入2后变成
B={2},{1,2},{2,2},{1,2,2}
发现A和B中都出现了{2},{1,2},这是因为重复的元素2使得{},{1}加入2后发生重复
一种简单解法:将结果放入vector中,然后插入set来去重
可以这样,如果当前待插入的元素和前一个元素一样,代表当前元素是重复的,
然后对A进行遍历时,查找子集中如果没有当前元素,就跳过该处理【因为这种情况会导致重复】
这样的时间成本在于:从A的每个子集都需要查找当前元素是否出现过,如果将该子集的每个元素放入哈希map,
每个子集查找时间为O(n),所有子集。
如果更一般地可以认为出现重复的在前半段的A的子集中,但是这得基于对给定nums进行排序,使得重复元素相邻。
但是如果出现第3个重复的元素,那么重复的区间位置是:2/3 * n的位置过滤掉
A={},{1},{2},{1,2},{2,2},{1,2,2}
B={2,2},{1,2,2},{2,2,2},{1,2,2,2}
发现含有<=1个2的都会重复
A={},{1},{2},{1,2},{2,2},{1,2,2},{2,2,2},{1,2,2,2}
把含有<=2个2的都过滤,{1,2,2,2,2},留下后面1/4个元素,当有4个重复元素;留下后面1/3,当有3个元素重复
当有1个元素,留下后面1/1

总结规律:
统计当前元素已经出现的次数记为n,生成前面的子集A后,另子集B=A,对B的后面1/n部分的子集中每个子集中
插入新的元素,设B的长度为L,删除(0 , (n-1)/n * L)的子集  

输入:
1 2 2
1 2 2 2
输出:
{},{1},{2},{1,2},{2,2},{1,2,2}
{},{1},{2},{1,2},{2,2},{1,2,2},{2,2,2},{1,2,2,2}

关键:
1 总结规律:
统计当前元素已经出现的次数记为n,生成前面的子集A后,另子集B=A,对B的后面1/n部分的子集中每个子集中
插入新的元素,设B的长度为L,删除(0 , L *(n-1)/n )的子集
*/
class Solution {
public:
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
		vector<vector<int>> results;
		vector<int> result;
		results.push_back(result);
		if(nums.empty())
		{
			return results;
		}
		//对元素排序,使得重复的元素相邻
		sort(nums.begin() , nums.end());
		int size = nums.size();
		int count = 1;//统计某个元素出现次数
		int value = nums.at(0) - 1;
		int len;
		int pos ;
		for(int i = 0 ; i < size ; i++)
		{
			if(i)
			{
				//如果出现重复元素,和之前元素相等
				if(nums.at(i) == nums.at(i-1))
				{
					count++;
				}
				else
				{
					count = 1;
				}
				//接下来只在后面1/count * len 处插入
				vector< vector<int> > tempResults;
				len = results.size();
				pos = len * (count - 1) / count; //乘法放在前面,否则小数转换变成0了
				//删除重复的子集,重复子集对应下标是在0到 (count - 1) / count * len - 1之中
				tempResults.insert(tempResults.begin() , results.begin() + pos , results.end() );
				len = tempResults.size();
				//每个子集中插入当前元素
				for(int j = 0 ; j < len ; j++)
				{
					tempResults.at(j).push_back(nums.at(i));
				}
				results.insert(results.end() , tempResults.begin() , tempResults.end());
			}
			//首个元素肯定不会重复,直接拷贝结果
			else
			{
				vector< vector<int> > tempResults(results);
				len = tempResults.size();
				//每个子集中插入当前元素
				for(int j = 0 ; j < len ; j++)
				{
					tempResults.at(j).push_back(nums.at(i));
				}
				results.insert(results.end() , tempResults.begin() , tempResults.end());
			}
		}
		return results;
    }
};

void print(vector<vector<int> >& result)
{
	if(result.empty())
	{
		cout << "no result" << endl;
		return;
	}
	int size = result.size();
	int len = 0;
	for(int i = 0 ; i < size ; i++)
	{
		len = result.at(i).size();
		cout << "{";
		for(int j = 0 ; j < len ; j++)
		{
			cout << result.at(i).at(j) << " " ;
		}
		cout << "} ," ;
	}
	cout << endl;
}

void process()
{
	 vector<int> nums;
	 int value;
	 int num;
	 Solution solution;
	 vector<vector< int > > result;
	 while(cin >> num )
	 {
		 nums.clear();
		 for(int i = 0 ; i < num ; i++)
		 {
			 cin >> value;
			 nums.push_back(value);
		 }
		 result = solution.subsetsWithDup(nums);
		 print(result);
	 }
}

int main(int argc , char* argv[])
{
	process();
	getchar();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值