zoukankan      html  css  js  c++  java
  • [LeetCode] Subsets II [32]

    题目

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

    原题链接(点我)

    解题思路

    这个题非常subsets这个题一样。只是这里同意给出的集合中含有反复元素,对于这个条件之须要加一个推断条件就能够了,其余代码和Subsets都一样。

    代码实现

    class Solution {
    public:
        vector<vector<int> > subsetsWithDup(vector<int> &S) {
            vector<vector<int> > ret;
            sort(S.begin(), S.end());
            helper(0, S, vector<int>(), ret);
            return ret;
        }
        
        void helper(int start, const vector<int>& S, vector<int> part, vector<vector<int> >& ret){
            if(start == S.size()) return;
            if(start == 0) ret.push_back(part);
            for(int i=start; i<S.size(); ++i){
                // 这里加了个推断。就能够避免反复组合
                if(i>start && S[i] == S[i-1]) continue;
                part.push_back(S[i]);
                ret.push_back(part);
                helper(i+1, S, part, ret);
                part.pop_back();
            }
        }
    };

    假设你认为本篇对你有收获,请帮顶。
    另外。我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
    你能够搜索公众号:swalge 或者扫描下方二维码关注我

    (转载文章请注明出处: http://blog.csdn.net/swagle/article/details/30221841 )

  • 相关阅读:
    使用Nginx搭建http服务器
    (七)Docker搭建httpd集群
    zlib库对文件进行压缩和解压操作
    (一)Apache Thrift 的使用
    (一)select、poll、epoll
    (十三)备忘录模式
    (十二)命令模式
    (十一)迭代器模式
    centos下利用httpd搭建http服务器方法
    shell快捷键
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6723819.html
Copyright © 2011-2022 走看看