combination-sum-ii

2019-07-17  本文已影响0人  DaiMorph
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);
    }
};
上一篇下一篇

猜你喜欢

热点阅读