zoukankan      html  css  js  c++  java
  • 90 [LeetCode] Subsets2

    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],
      []
    ]





    class Solution {
    public:
      vector<vector<int>> subsetsWithDup(vector<int> &S) {
            vector<vector<int>> totalset = {{}};
            sort(S.begin(),S.end());
            for(int i=0; i<S.size();){
                int count = 0; // num of elements are the same
                while(count + i<S.size() && S[count+i]==S[i])  count++;
                int previousN = totalset.size();
                for(int k=0; k<previousN; k++){
                    vector<int> instance = totalset[k];
                    for(int j=0; j<count; j++){
                      
                        instance.push_back(S[i]);
                          totalset.push_back(instance);
                        
                    }
                }
                i += count;
            }
            return totalset;
            } 
    };
  • 相关阅读:
    PHP+AJAX 验证码验证用户登录
    2014-05-09 总结
    2014-05-08 总结(补充)
    2014-05-08 总结
    2014-05-07 总结
    14-6-2
    php
    5-23
    PHP
    5-22
  • 原文地址:https://www.cnblogs.com/250101249-sxy/p/10426881.html
Copyright © 2011-2022 走看看