Leetcode 77. Combinations 39. Combination Sum

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

For example,
If n = 4 and k = 2, a solution is:

[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4], 
]

DFS輕松解決,需要注意的是這個(gè)是Combination不是Permutation

vector<vector<int>> combine(int n, int k) {
    vector<vector<int>> result;
    vector<int> curr;
    
    if(n == k){
        result.push_back(curr);
        for(int i = 1;i <= k;i++){
            result[0].push_back(i);
        }
        return result;
    }
    
   
    helper(result,curr,1,n,k);
  
    return result;
    
}


void helper(vector<vector<int>>& res,vector<int>& cur,int start,int n,int k){
    if(cur.size() == k){
        res.push_back(cur);
        return ;
    }
    
    for(int i = start;i <= n ;i++){
        if(find(cur.begin(),cur.end(),i) == cur.end()){
            cur.push_back(i);
            helper(res,cur,i + 1,n,k);
            cur.pop_back();
        }    
    }
}

Given a set of candidate numbers (C) (without duplicates) 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.

Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,

A solution set is: 
[
  [7],
  [2, 2, 3]
]

方法和上面那個(gè)很相似

vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
    vector<vector<int>> res;
    vector<int> cur;
    helper(res,cur,candidates,target);
    
    return res;
}

void helper(vector<vector<int>>& res,vector<int>& cur,const vector<int>& info,int sum){
    if(sum == 0){
        
        for(int i = 0;i < res.size();i++){
            if(is_permutation(res[i].begin(),res[i].end(),cur.begin())){
                return ;
            }
        }
        res.push_back(cur);
        return ;
    }   
    
    for(int i = 0;i < info.size();i++){
        if(info[i] <= sum){
            cur.push_back(info[i]);
            helper(res,cur,info,sum - info[i]);
            cur.pop_back();
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,916評(píng)論 0 33
  • 閱讀杜拉拉升職記175頁(yè)到192頁(yè) 感悟.杜拉拉升職記是我們HR經(jīng)理推薦,開始以為是小說(shuō),其實(shí)不然。職場(chǎng)人士必...
    汩雨貓閱讀 355評(píng)論 0 0
  • 設(shè)計(jì)模式是什么?設(shè)計(jì)模式是經(jīng)過(guò)總結(jié)、優(yōu)化的,對(duì)我們經(jīng)常會(huì)碰到的一些編程問(wèn)題的可重用解決方案。一個(gè)設(shè)計(jì)模式并不像一個(gè)...
    靜熙老師哈哈哈閱讀 625評(píng)論 0 7
  • 【聊?感】 給好友打了兩個(gè)小時(shí)電話。聊聊自己的事情想看他的觀點(diǎn)。我很認(rèn)同。是因?yàn)闀线@么說(shuō)。他也這么說(shuō)。其實(shí)我也認(rèn)...
    言十年閱讀 228評(píng)論 0 0
  • 03 A2:見面,s床。后來(lái)她打電話約我見面,我把第一次見面的地點(diǎn)框架到我家里。在沒見面以前她就對(duì)我很有感覺了,見...
    一條老白狼閱讀 493評(píng)論 0 0

友情鏈接更多精彩內(nèi)容