首页 > 其他 > 详细

LeetCode Subsets II

时间:2014-07-19 13:26:08      阅读:300      评论:0      收藏:0      [点我收藏+]
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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!