zoukankan      html  css  js  c++  java
  • LeetCode-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],
      []
    ]
    普通遍历
    class Solution {
    public:
        vector<vector<int> > subsetsWithDup(vector<int> &S) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            set<vector<int> > res;
            vector<int> one;
            vector<int> oneone;
            int i;
            vector<int> check;
            for(i=0;i<S.size();i++)check.push_back(0);
            i=0;
            while(true){
                if(check[i]==2){
                    i--;
                    if(i<0)break;
                    one.pop_back();
                }
                else{
                    if(check[i]==1){
                        one.push_back(S[i]);
                    }
                    check[i]++;
                    i++;
                    if(i==S.size()){
                        oneone=one;
                        sort(oneone.begin(),oneone.end());
                        res.insert(oneone);
                        i--;
                    }
                    else{
                        check[i]=0;
                    }
                }
                
            }
            set<vector<int> >::iterator it;
            vector<vector<int> > ret;
            for(it=res.begin();it!=res.end();it++){
                ret.push_back(*it);
            }
            return ret;
        }
    };
  • 相关阅读:
    nyoj 202 红黑树
    nyoj 237 游戏高手的烦恼
    nyoj 203 三国志
    nyoj 118 修路方案
    nyoj 714 Card Trick
    nyoj 710 外星人的供给站
    nyoj 712探 寻 宝 藏
    nyoj 709 异 形 卵
    nyoj 711 最舒适的路线
    HTML5表格简单应用案例之[招聘需求表]
  • 原文地址:https://www.cnblogs.com/superzrx/p/3332062.html
Copyright © 2011-2022 走看看