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

combination sum

时间:2015-08-16 16:34:24      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int>> path;
        vector<int> p;
        sort(candidates.begin(),candidates.end());
        help(0,target,candidates,path,p);
        return path;
    }
    
    void help(int index,int target,vector<int>& candidates,vector<vector<int>> &path,vector<int> &p){
            int i;
            for(i=index;i<candidates.size();i++){
            if(target < candidates[i]) {
                return;
            }
            
            p.push_back(candidates[i]);
            
            if(target == candidates[i]) {
                path.push_back(p);
                p.pop_back();
                return;
            }
            help(i,target-candidates[i],candidates,path,p);
            p.pop_back();
        }
        return;
        
    }
};

 

combination sum

标签:

原文地址:http://www.cnblogs.com/julie-yang/p/4734508.html

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