日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Leetcode39.Combination Sum组合总和

發(fā)布時(shí)間:2024/4/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Leetcode39.Combination Sum组合总和 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

給定一個(gè)無(wú)重復(fù)元素的數(shù)組?candidates?和一個(gè)目標(biāo)數(shù)?target?,找出?candidates?中所有可以使數(shù)字和為?target?的組合。

candidates?中的數(shù)字可以無(wú)限制重復(fù)被選取。

說(shuō)明:

  • 所有數(shù)字(包括?target)都是正整數(shù)。
  • 解集不能包含重復(fù)的組合。?

示例?1:

輸入: candidates = [2,3,6,7], target = 7, 所求解集為: [ [7], [2,2,3] ]

示例?2:

輸入: candidates = [2,3,5], target = 8, 所求解集為: [ ? [2,2,2,2], ? [2,3,3], ? [3,5] ]

?

先排序,后剪枝,避免重復(fù)。

這類(lèi)問(wèn)題解決重復(fù)的操作一般是先進(jìn)行排序。

?

?

bool cmp1(int x, int y) {return x < y; }class Solution { public:vector<vector<int> > res;vector<vector<int> > combinationSum(vector<int>& candidates, int target){int len = candidates.size();sort(candidates.begin(), candidates.end(), cmp1);vector<int> temp;DFS(candidates, target, temp, 0);return res;}void DFS(vector<int> candidates, int val, vector<int> &v, int cnt){if(val == 0){res.push_back(v);return;}for(int i = cnt; i < candidates.size(); i++){if(candidates[i] <= val){v.push_back(candidates[i]);DFS(candidates, val - candidates[i], v, i);v.pop_back();}}} };

轉(zhuǎn)載于:https://www.cnblogs.com/lMonster81/p/10433878.html

總結(jié)

以上是生活随笔為你收集整理的Leetcode39.Combination Sum组合总和的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。