zoukankan      html  css  js  c++  java
  • Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets.

    Note:

    • Elements in a subset must be in non-descending order.
    • The solution set must not contain duplicate subsets.

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

    [
      [2],
      [1],
      [1,2,2],
      [2,2],
      [1,2],
      []
    ]
    
    Hide Tags
     Array Backtracking
     
    与  1 相同,需考虑重复
    class Solution {
    private:
        vector<vector<int> > ret;
    public:
        void generate(vector<int> vet,vector<int> &S,int i){
            if(i==S.size()){
                for(int k=0;k<ret.size();k++){
                    if(vet==ret[k])          //去除重复
                        return;
                }
                ret.push_back(vet);
                return;
            }
            generate(vet,S,i+1);        //相当于取右子树
            vet.push_back(S[i]);             
            generate(vet,S,i+1);       //相当于取左子树
        }
        vector<vector<int> > subsetsWithDup(vector<int> &S) {  
            sort(S.begin(),S.end());
            generate(vector<int>(),S,0);
            return ret;
        }
    };
  • 相关阅读:
    点击有惊喜
    模态框案例
    DOM操作
    定时器
    函数和object
    shell 判断文件出现次数
    shell 判断路径
    shell 循环数组
    shell 判断为空打印
    shell 示例1 从1叠加到100
  • 原文地址:https://www.cnblogs.com/li303491/p/4112629.html
Copyright © 2011-2022 走看看