LeetCode的medium題集合(C++實現)五

來源:互聯網
上載者:User

標籤:遞迴   c++   leetcode   

1 Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times.
可以採用遞迴的方法解決這個問題,當找到一組數之和等於目標後將這組數加入容器,然後返回;當一組數之和大於目標,立即返回;當小於目標繼續遞迴。

void dfscombine(vector<int>& candidates,int level,int& sum,int target,vector<int>& mid,vector<vector<int> >& result)    {        if(sum>target) return;        else if(sum==target)        {           result.push_back(mid);           return;        }        else        {          for(int i=level;i<candidates.size();i++)          {              sum+=candidates[i];              mid.push_back(candidates[i]);              dfscombine(candidates,i,sum,target,mid,result);              mid.pop_back();              sum-=candidates[i];          }        }    }    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {        vector<int> mid;          vector<vector<int> > result;          sort(candidates.begin(),candidates.end());        int level=0,sum=0;        dfscombine(candidates,level,sum,target,mid,result);        return result;    }

2 Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination.
該題與上一題的區別是給定的資料集中的資料只能用一次,我只需將上題中遞迴的參數level=i 改為 level=i+1 即可。同時,與前面講過的3sum等問題類似,防止從容器中pop_back() 出來的數與即將加入容器中的數相等而造成重複。

void dfscombine(vector<int>& candidates,int level,int& sum,int target,vector<int>& mid,vector<vector<int> >& result)    {        if(sum>target) return;        else if(sum==target)           result.push_back(mid);        else        {          for(int i=level;i<candidates.size();i++)          {              sum+=candidates[i];              mid.push_back(candidates[i]);              dfscombine(candidates,i+1,sum,target,mid,result); //保證一個元素只用一次              mid.pop_back();              sum-=candidates[i];              while(i<candidates.size()-1 && candidates[i]== candidates[i+1]) i++;  //防止重複          }        }    }    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {        vector<int> mid;          vector<vector<int> > result;          sort(candidates.begin(),candidates.end());        int level=0,sum=0;        dfscombine(candidates,level,sum,target,mid,result);        return result;    }

LeetCode的medium題集合(C++實現)五

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.