美文网首页
combination-sum-ii

combination-sum-ii

作者: DaiMorph | 来源:发表于2019-07-17 21:39 被阅读0次
class Solution {
public:
    vector<vector<int> > combinationSum2(vector<int> &num, int target) {
        vector<vector<int>>res;
        vector<int>path;
        set<vector<int>>tempres;
        sort(num.begin(),num.end());
        dfs(0,target,num,path,tempres);
        for(auto it=tempres.begin();it!=tempres.end();it++)res.push_back(*it);
        return res;
    }
    void dfs(int now,int gap,vector<int>num,vector<int>&path,set<vector<int>>&res)
    {
        if(gap==0)
        {
            res.insert(path);
            return;
        }
        if(gap<0||now>=num.size())return;
        path.push_back(num[now]);
        dfs(now+1,gap-num[now],num,path,res);
        path.pop_back();
        dfs(now+1,gap,num,path,res);
    }
};

相关文章

网友评论

      本文标题:combination-sum-ii

      本文链接:https://www.haomeiwen.com/subject/pvfhlctx.html