首页 > 其他 > 详细

LeetCode OJ:Subsets II

时间:2014-01-23 10:10:51      阅读:414      评论:0      收藏:0      [点我收藏+]

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:
    void dfs(vector<int> &S,int start,vector<int> &path,vector<vector<int>> &result){
        result.push_back(path);
        for(int i=start;i<S.size();i++){
            if(i!=start&&S[i]==S[i-1])continue;
            path.push_back(S[i]);
            dfs(S,i+1,path,result);
            path.pop_back();
        }
    }
    vector<vector<int> > subsetsWithDup(vector<int> &S) {
        vector<vector<int>> result;
        vector<int> path;
        sort(S.begin(),S.end());
        dfs(S,0,path,result);
        return result;
    }
};


LeetCode OJ:Subsets II

原文:http://blog.csdn.net/starcuan/article/details/18681435

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