Beats 24.75%. Easy Understandable.


  • 0
    N
    class Solution {
    public:
        vector<vector<int> >vv;
        vector<int>v;
        map<vector<int>,int>mp;
        void recur(vector<int>& candidates, int target,int i,int sum)
        {
            if(sum==target)
            {
                if(mp[v]==0)
                {
                    vv.push_back(v);
                    mp[v]=1;
                }
                return;
            }
            if(i==candidates.size()||sum>target)return;
            recur(candidates,target,i+1,sum);
            v.push_back(candidates[i]);
            recur(candidates,target,i,sum+candidates[i]);
            v.pop_back();
        }
        vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
            sort(candidates.begin(),candidates.end());
            recur(candidates,target,0,0);
            return vv;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.