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

Subsets-II

时间:2014-12-22 17:56:21      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

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

依然回溯,补充一个条件即可:

void subsets(vector<int> vecInt)
{
    sort(vecInt.begin(), vecInt.end());
    vector<int> vecWorker;
    subsetsHelper(vecWorker, vecInt, 0);
}

void subsetsHelper(vector<int>& vecWorker, const vector<int>& vecInt, int pos)
{
    outputToResult(vecWorker);

    for (int i = pos; i < vecInt.length(); i++)
    {
        if (i != pos && vecInt[i] == vecInt[i-1])
            continue;

        vecWorker.push_back(vecInt[i]);
        subsetsHelper(vecWorker, vecInt, i+1);
        vecWorker.pop_back();
    }
}

 

Subsets-II

标签:

原文地址:http://www.cnblogs.com/litao-tech/p/4178620.html

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