Subsets II - LintCode

描述
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).

子集中的每个元素都是非降序的
两个子集间的顺序是无关紧要的
解集中不能包含重复子集

样例
Input: [1,2,2]
Output:

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

挑战
你可以同时用递归与非递归的方式解决么?

思路

#ifndef C18_H
#define C18_H
#include<vector>
#include<set>
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(const int a, const int b)
{
    return a > b;
}
class Solution {
public:
    /**
    * @param nums: A s of numbers.
    * @return: A list of lists. All valid subss.
    */
    vector<vector<int>> subsetsWithDup(vector<int> &nums) {
        // write your code here
        if (nums.empty())
            return{ {} };
        //对nums排序使其单调不增
        sort(nums.begin(), nums.end(), cmp);
        //处理nums的末尾元素,当s和res为空时,添加{},{nums.back()}
        //当s不为空,遍历res,将nums.back()添加到元素并将元素添加到res
        while (!nums.empty())
        {
            if (s.empty())
            {
                s.insert({});
                s.insert({ nums.back() });
                res.push_back({});
                res.push_back({ nums.back() });
            }
            else
            {
                int len = res.size();
                for (int i = 0; i < len; ++i)
                {
                    vector<int> temp = res[i];
                    temp.push_back(nums.back());
                    if (s.find(temp) == s.end())
                    {
                        res.push_back(temp); 
                        s.insert(temp);
                    }
                }
            }
            nums.pop_back();
        }
        return res;
    }
    set<vector<int>> s;//存放已经出现的结果
    vector<vector<int>> res;//存放最终结果
};


#endif

猜你喜欢

转载自blog.csdn.net/zhaohengchuan/article/details/80914007