zoukankan      html  css  js  c++  java
  • 90. Subsets II

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

    Note: The solution set must not contain duplicate subsets.

    Example:

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

    AC code:

    class Solution {
    private:
        void helper(int begin, int len, vector<int> temp, vector<vector<int>>& res, vector<int> nums) {
            res.push_back(temp);
            for (int i = begin; i < len; ++i) {
                if (i != begin && nums[i] == nums[i-1]) continue;
                temp.push_back(nums[i]);
                helper(i+1, len, temp, res, nums);
                temp.pop_back();
            }
        }
        
    public:
        vector<vector<int>> subsetsWithDup(vector<int>& nums) {
            vector<int> temp;
            vector<vector<int>> res;
            sort(nums.begin(), nums.end());
            int len = nums.size();
            helper(0, len, temp, res, nums);
            return res;
        }
    };
    
    Runtime: 4 ms, faster than 100.00% of C++ online submissions for Subsets II.
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    wifi 与 以太网 以及 修改网络查看网络
    git 与 gitHub 与 gitLab ,git常用5个命令
    花生壳
    诗词古文
    基金龙虎榜
    osm_mano安装
    db2快速删除大表数据(亲测可用)
    行列转换
    DB2表空间
    表分区,和分表区别
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9866112.html
Copyright © 2011-2022 走看看