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;
        }
    };
  • 相关阅读:
    git使用
    javascript关于事件与闭包
    Ajax和跨域
    通过触发器进行的操作
    30分钟学会jquery插件
    那些年用过的jquery插件
    网页设计常用网页技巧
    XML操作
    效果A:浏览器跳转以及判断来路客户信息
    数据库
  • 原文地址:https://www.cnblogs.com/superzrx/p/3332062.html
Copyright © 2011-2022 走看看