首页 > 其他 > 详细

Subsets 集合子集 回溯

时间:2014-11-21 01:17:16      阅读:216      评论:0      收藏:0      [点我收藏+]

Given a set of distinct integers, 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,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

 

Hide Tags
 Array Backtracking Bit Manipulation
 
递归,放与不放的问题。转化成生成树

bubuko.com,布布扣

class Solution {
private:
    vector<vector<int> > ret;
public:
    void generate(vector<int> vet,vector<int> &S,int i){
        if(i==S.size()){
            ret.push_back(vet);
            return;
        }
        generate(vet,S,i+1);        //相当于取右子树
        vet.push_back(S[i]);             
        generate(vet,S,i+1);       //相当于取左子树
    }
    vector<vector<int> > subsets(vector<int> &S) {
        ret.clear();
        sort(S.begin(),S.end());
        vector<int> vet;
        generate(vet,S,0);
        return ret;
    }
};

 

Subsets 集合子集 回溯

原文:http://www.cnblogs.com/li303491/p/4111860.html

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