码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Subsets II

时间:2014-07-19 11:35:37      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   re   

class Solution {
public:
    vector<vector<int> > subsetsWithDup(vector<int> &S) {
        int len = S.size();
        vector<vector<int> > res;
        vector<int> subset;
        if (len < 1) {
            res.push_back(subset);
            return res;
        }
        sort(S.begin(), S.end());
        vector<int> stat;
        vector<int> nums;

        int count = 1;
        int cur = S[0];
        int last = cur;
        for (int i=1; i<len; i++) {
            cur = S[i];
            if (cur != last) {
                stat.push_back(count);
                nums.push_back(last);
                last = cur;
                count = 0;
            }
            count++;
        }
        stat.push_back(count);
        nums.push_back(last);

        dfs(nums, stat, res, subset, 0);
    }

    void dfs(vector<int> &nums, vector<int> &stat, vector<vector<int> > &res, vector<int> &subset, int k) {
        if (k >= nums.size()) {
            res.push_back(subset);
            return;
        }

        int cnt = stat[k];

        int old = subset.size();

        for (int i=0; i <= cnt; i++) {
            dfs(nums, stat, res, subset, k + 1);
            subset.push_back(nums[k]);
        }
        subset.resize(old);
    }
};

常规dfs

LeetCode Subsets II,布布扣,bubuko.com

LeetCode Subsets II

标签:style   blog   color   io   for   re   

原文地址:http://www.cnblogs.com/lailailai/p/3854572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!